immutable-hash
An immutable hash structure with delta journaling
Example
var assert = var ImmutableHash = var hash = var hash2 = hashassert var hash3 = hash2assert var hash4 = hash3assertassert var hash5 = hash4assertassert var hash6 = hash5assertassert var hash7 = hash6assert var hash8 = hash7assert var hash9 = hash8assert var hash10 = hash9assert
Todo
diff()
Makemap()
andfilter()
callpatch()
once.- Improve performance & benchmarks
Make integration() test at least as fast as diffpatcher
Benchmark
$ npm run bench
> immutable-hash@0.1.2 bench /home/raynos/Documents/immutable-hash
> node ./benchmarks
# ImmutableHash patch()
# --- patch(foo.bar, baz) x 765,697 ops/sec @ 1306 milliseconds elapsed
# --- patch([foo, bar], baz) x 738,007 ops/sec @ 1355 milliseconds elapsed
# --- patch({ foo: { bar: baz } }) x 569,476 ops/sec @ 1756 milliseconds elapsed
# ImmutableHash patch(key, value)
# --- patch(foo, bar) at 0 x 862,069 ops/sec @ 116 milliseconds elapsed
# --- patch(foo, bar) at 10 x 152,905 ops/sec @ 654 milliseconds elapsed
# --- patch(foo, bar) at 100 x 82,372 ops/sec @ 1214 milliseconds elapsed
# --- patch(foo, bar) at 1000 x 65,274 ops/sec @ 1532 milliseconds elapsed
# --- nested patch(foo, bar) at 1000 x 47,755 ops/sec @ 2094 milliseconds elapsed
# integration(0)
# --- ImmutableHash x 28,409 ops/sec @ 352 milliseconds elapsed
# --- diffpatcher x 27,701 ops/sec @ 361 milliseconds elapsed
# integration(10)
# --- ImmutableHash x 9,225 ops/sec @ 542 milliseconds elapsed
# --- diffpatcher x 7,062 ops/sec @ 708 milliseconds elapsed
# integration(100)
# --- ImmutableHash x 1,259 ops/sec @ 794 milliseconds elapsed
# --- diffpatcher x 1,221 ops/sec @ 819 milliseconds elapsed
# integration(1000)
# --- ImmutableHash x 81 ops/sec @ 2466 milliseconds elapsed
# --- diffpatcher x 111 ops/sec @ 1804 milliseconds elapsed
ImmutableHash is slower at larger size hashes
Documentation
ImmutableHash(initialState)
ImmutableHash :: initial:Object<String, Any> -> ImHash
Creates an ImmutableHash with optionally initial state.
var hash = var res = hash // { foo: "1", bar: { baz: "2" } }var hash2 = var res2 = hash2 // { foo: "1", bar: { baz: "2" } }
hash().patch(path, value)
patch :: ImHash -> parts:[String] -> value:Any -> ImHashpatch :: ImHash -> path:String -> value:Any -> ImHashpatch :: ImHash -> delta:Object<String, Any> -> ImHash
Returns a new ImHash with the patch applied to it
var hash = var hash2 = hashvar res2 = hash2 // { bar: { baz: "2" } }var hash3 = hashvar res3 = hash3 // { bar: { baz: "2" } }var hash4 = hashvar res4 = hash4 // { foo: "1", baz: { baz: "2" } }
hash().toJSON()
toJSON :: ImHash -> Object
Returns a normal JavaScript object representation of the ImHash
var hash = var res = hash // { foo: "1", bar: { baz: "2" } }
hash().get(key)
get :: ImHash -> String -> Any
Returns the value associated with the key. Can either be a key or a nested query key.
var hash = var foo = hash // "1"var bar = hash // <ImHash>var baz1 = bar // "2"var baz2 = hash // "2"
hash().has(key)
has :: ImHash -> String -> Boolean
Returns a boolean indicating whether the key is found. Can either be a key or a nested query key.
var hash = var foo1 = hash // truevar foo2 = hash // falsevar bar = hash // truevar baz1 = hash // falsevar baz2 = bar // truevar baz3 = hash // truevar baz4 = hash // false
hash().map(path, lambda)
map :: ImHash<String, A> -> lambda:(A -> B) -> ImHash<String, B>map :: ImHash -> path:String -> (A -> B) -> ImHash
Takes a path, get's the ImHash hash
at that location. Then patches it
by calling a lambda function on each value in it and replacing
that key with the returned value.
Returns an ImHash with hash
replaced with the patched hash.
var state = var toggled = Math < 05 ? true : falsevar newState = state
hash().filter(path, lambda)
filter :: ImHash<String, A> -> lambda:(A -> Boolean) -> ImHash<String, A>filter :: ImHash -> path:String -> (A -> Boolean) -> ImHash
Takes a path, get's the ImHash hash
at that location. It then patches
it by calling a lambda function on each value in it and if the lambda
returns false that value get's removed.
Returns an ImHash with hash
replaced with the patched hash.
var hash = var newState = state
hash().diff(otherHash)
diff:: this:ImHash -> other:ImHash -> Object
returns an object representation of the values that are different between the two hashes.
This is optimized for the case where this
is created by
patching other
. Which means you can do an very efficient
curr.diff(prev)
call.
var hash = var hash2 = hashvar diff = hash2 // { bar: "baz" }
Installation
npm install immutable-hash
Contributors
- Raynos