Coder Social home page Coder Social logo

mootable / hashmap Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 8.26 MB

Providing fast at scale HashMap, LinkedHashMap, and Higher Order Functions to any iterable, such as Array, Map or Set. Tested, and Benchmarked, Issues & PRs welcomed.

License: MIT License

JavaScript 93.10% CSS 4.02% HTML 2.88%
hashmap linkedhashmap higher-order-functions map filter concat reduce hashing hamt defered-execution

hashmap's Introduction

The Moo Tableau Coverage Status Known Vulnerabilities Dependencies Inline docs

HashMap & LinkedHashMap

Description

This project provides HashMap and LinkedHashMap classes that works both on Node.js and the browser.

  • They are both implementations of a simplified HAMT like hash trie
  • It uses a modified Murmer 3 algorithm for generating hashes. This ensures the widest possible spread across all buckets.
  • As per spec, the basic Hashmap is not guaranteed to meet order of insertion when iterating over it. If you want guaranteed insertion order when iterating, use LinkedHashMap.
  • The keys are truly typed and unique, this means 1 !== "1".

Choose your map wisely.

  • When choosing a collection it is worth understanding the problem you are trying to solve.
  • Native JS Map for small numbers of entries, will be significantly faster.
  • However once the map reaches 1'000 or more the Mootable Hashmap really shows its strengths. It utilizes more memory, to do this.
  • The Native JS Map is likely to have improved speed characteristics if repeating operations in a loop, via things such as JIT compilation. It is worth benchmarking to see if Map works better for you in those situations.

Installation

NPM

Using npm:

$ npm install @mootable/hashmap

You can download the last stable version from the releases page.

If you like risk, you can download the latest master version, it's usually stable.

To run the tests:

$ npm test

To run the benchmarks: (Ensure you have the memory to run them)

  • If you don't you can reduce the memory size (in MB) accordingly --max_old_space_size and remove the last items in MAP_SIZES

    $ node --max_old_space_size=24576 --expose-gc test\benchmark.js

API Documentation

  • For Full HTML Documentation with Examples please visit Here
  • For Full Markdown Documentation please visit Here

Importing

Node ESModules

 import {HashMap, LinkedHashMap} from '@mootable/hashmap';

Node CommonJS

 const {HashMap, LinkedHashMap} = require('@mootable/hashmap');

Browser

<!-- Or one of the other provided alternates (see dist folder) -->
<script src="pathToFile/hashmap.umd.min.js"></script>
<script type="text/javascript">
    // By Default Mootable is on the global scope.
    const HashMap = Mootable.HashMap;
    const LinkedHashMap = Mootable.LinkedHashMap;
</script>

HashMap constructor

This HashMap is backed by a hashtrie.

  • new HashMap() creates an empty hashmap
  • new HashMap(copy:Iterable) creates a hashmap which is a copy of the provided iterable.
    1. copy either
    • an object that provides a forEach function with the same signature as Map.forEach, such as Map or this HashMap and LinkedHashMap
    • or a 2 dimensional key-value array, e.g. [['key1','val1'], ['key2','val2']].

LinkedHashMap constructor

LinkedHashMap maintains insertion order of keys, it has a slightly larger memory footprint and is a little slower.

  • new LinkedHashMap() creates an empty linked hashmap
  • new LinkedHashMap(copy:Iterable) creates a linked hashmap which is a copy of the provided iterable.
    1. copy either
    • an object that provides a forEach function with the same signature as Map.forEach, such as Map or this HashMap and LinkedHashMap
    • or a 2 dimensional key-value array, e.g. [['key1','val1'], ['key2','val2']].

Examples

HashMap and LinkedHashMaps, have the same constructor options, and are created in the same way..

Create an empty HashMap

 const hashmap = new HashMap();
 // hashmap.size === 0;

Create a LinkedHashMap from an array of key value pairs

const arr = [[1,'value1'],[2,'value2'],[3,'value3']];
const hashmap = new LinkedHashMap(arr);
// hashmap.size === 3;

Create HashMap from another map

const map = new Map([[1,'value1'],[2,'value2'],[3,'value3']])
const hashmap = new HashMap(map);
// hashmap.size === 3;

Create LinkedHashMap from another HashMap

const first = new HashMap([[1,'value1'],[2,'value2'],[3,'value3']])
const hashmap = new LinkedHashMap(first);
// hashmap.size === 3;

Create a HashMap from a class with symbol

class MyIterable {
    *[Symbol.iterator] () {
        yield ["key1", "value1"];
        yield ["key2", "value2"];
        yield ["key3", "value3"];
        yield ["key4", "value4"];
    }
}
const iterable = new MyIterable();
const hashmap = new HashMap(iterable);
// hashmap.size === 4;
// it doesn't have to be a generator, an iterator works too.

Create a LinkedHashMap from an object with an entries' generator function

const entriesObj = {
    entries: function* () {
        yield ["key1", "value1"];
        yield ["key2", "value2"];
        yield ["key3", "value3"];
        yield ["key4", "value4"];
    }
}
const hashmap = new LinkedHashMap(entriesObj);
// hashmap.size === 4;
// it doesn't have to be a generator, an iterator works too.

Create HashMap from an object with a forEach function

const forEachObj = {
    forEach: (callback, ctx) => {
        for (let i = 1; i <= 4; i++) {
            callback.call(ctx, 'value' + i, 'key' + i);
        }
    }
};
const hashmap = new HashMap(forEachObj);
// hashmap.size === 4;

Benchmarks

  • Current Benchmarks can be found under benchmark_results.
    • The default benchmark does a single set, get and delete against a hashmap of a specific size. It does this thousands of times, and finds an approximate average.
    • If you would like me to include your library for benchmarking, raise an issue in github.
      • It must be in NPM
      • It must have an identical interface to JS Map
      • It must be fully written in JS. (Transpiling is acceptable) So that we can guarantee it works in the browser, not just node.

Benchmarks on version 1.0.7

Set Get And Delete

Background

  • This repository is a reimplemented version of the npm hashmap repository. It takes that implementation as a starting point, and moves it closer to the core functionality hashmaps are designed to achieve.
  • The tests have remained mostly the same, as has some documentation, everything else has changed. The interfaces have now diverged.

LICENSE

The MIT License (MIT)

Copyright (c) 2021 Jack Moxley

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF

hashmap's People

Contributors

devoncrouse avatar flesler avatar freddiecoleman avatar fresheneesz avatar jackmoxley avatar khrome avatar mvayngrib avatar nathanjang avatar psionski avatar qbolec avatar rafalwrzeszcz avatar thefourtheye avatar ulikoehler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

hashmap's Issues

Convert to ESModule, and transpile for other varients

We want:
ES Module untranspiled
Common JS for Node, minimaly untranspiled
Support for 99.5% of browsers, heavily transpiled Universal Module (UMD)
Support for browsers < 2 years., partialy transpiled Universal Module (UMD)

concat function

Given a map (first),
When I call concat on it with another map (second)
Then I get a new map (third) back with the contents of both.
And if ordered, then the order of third is one and then second following.

Overwrite key

use the overrides function to allow us to determine whether a key should also be overwritten, default false.
With push and unshift methods we may want to default this to true. To be thought through and decided.

Provide reverse iterators

Makes most sense for LinkedHashMaps, but will also work against HashMaps as they do have an order, just by hash,

Improve Optional => Option

I prefer the language semantics in Rust for this use case, than the Javaish ones.

Worth adding an iterable.

Provide Architectural Documentation

It wasn't clear, neither from documentation nor from code and neither from automatic tests, what methods of access support the order-preserving iteration.
Consider also the explicit reordering, such as https://docs.rs/indexmap/1.6.1/indexmap/map/struct.IndexMap.html#method.sort_keys and https://docs.rs/indexmap/1.6.1/indexmap/map/struct.IndexMap.html#method.sort_by

The docs mention using HAMT and trie, but I couldn't verify it from a cursory look at the code. IMHO there should be a mapping between the high-level documentation, such as the mention of HAMT, and the relevant code. Imagine going to Google Maps, zooming in and seeing entirely different location. The lack of connection or mapping between the different layers of documentation is similarly disconcerting. In order to be reusable ( https://youtu.be/pW-SOdj4Kkk ) software needs to allow for gradual zooming in and our, from architectural overview to code and back, IMHO.

Improve iteration

Iterate over Buckets better so that we can support streaming/functional programming, without preloading arrays.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.