Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
FastStringMap test2
(version: 0)
Comparing performance of:
test map vs test fast string map vs test set map vs test set fast string map vs obj
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
"use-strict"; const HASH_SIZE = 64, FNV_PRIME_64 = 1099511628211n, FNV_OFFSET_64 = 14695981039346656037n; let char; function FNV1aHash64(string) { const { length } = string; let hash = FNV_OFFSET_64, i = 0; for ( ; i < length; char = BigInt(string.charCodeAt(i++)), hash = BigInt.asUintN(HASH_SIZE, (hash ^ char) * FNV_PRIME_64) ); return hash; } const DEFAULT_TABLE_SIZE_L_SHIFT = 9; function fastRemoveElem(arr, i) { const tmp = arr[arr.length - 1]; arr[arr.length - 1] = arr[i]; arr[i] = tmp; arr.pop(); } // TODO: use mersenne primes for table size // TODO: handle grow // TODO: values method class FastStringMap { constructor() { this._size_factor = DEFAULT_TABLE_SIZE_L_SHIFT; const capacity = 0x2 << this._size_factor; this._capacity = BigInt(capacity); this._table = new Array(capacity); this.size = 0; } _indexOfKey(key) { return FNV1aHash64(key) & (this._capacity - 1n); } /** * @param key unique key * @returns the value stored in the key, or undefined if it doesnt exist */ get(key) { const bucket = this._table[this._indexOfKey(key)]; if (!bucket) { return undefined; } let { length } = bucket, i = 0; for (; i < length; ++i) { if (bucket[i][0] === key) { return bucket[i][1]; } } return undefined; } /** * @param key unique key * @param value the value to store * @returns this */ set(key, value) { let bucket, table = this._table, index = this._indexOfKey(key); if ((bucket = table[index]) !== undefined) { bucket.push([key, value]); } else { table[index] = [[key, value]]; } ++this.size; return this; } /** * @param key unique key * @returns this */ remove(key) { const bucket = this._table[this._indexOfKey(key)]; if (!bucket) { return this; } let { length } = bucket, i = 0; for (; i < length; ++i) { if (bucket[i][0] === key) { fastRemoveElem(bucket, i); return this; } } return this; } } var fastStringMap = new FastStringMap(); var map = new Map(); var obj = {};
Tests:
test map
for(let i=0; i<100; ++i){ map.set(i.toString(), i); } for(let i=0; i<100000; ++i){ map.get(i.toString()); }
test fast string map
for(let i=0; i<100; ++i){ fastStringMap.set(i.toString(), i); } for(let i=0; i<100000; ++i){ fastStringMap.get(i.toString()); }
test set map
for(let i=0; i<100; ++i){ map.set(i.toString(), i); }
test set fast string map
for(let i=0; i<100; ++i){ fastStringMap.set(i.toString(), i); }
obj
for(let i=0; i<100; ++i){ obj[i] = i; } for(let i=0; i<100000; ++i){ obj[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
test map
test fast string map
test set map
test set fast string map
obj
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the benchmark and its results. **Benchmark Overview** The benchmark is designed to measure the performance of different data structures: Maps (JavaScript's built-in Map object) and a custom implementation called FastStringMap. The test cases involve setting and getting values in each data structure, as well as removing values from the Map. **Data Structures Compared** 1. **Maps**: JavaScript's built-in Map object. 2. **FastStringMap**: A custom implementation of a hash-based map that uses a fixed-size array to store key-value pairs. **Options Compared** The benchmark compares the performance of the two data structures in three test cases: 1. `test set map`: Sets values in a Map and then retrieves them using the `get` method. 2. `test set fast string map`: Sets values in a FastStringMap and then retrieves them using the `get` method. 3. `obj`: A simple object literal with integer values, used to test access patterns. **Performance Results** The benchmark results show that the performance of the two data structures varies depending on the test case: 1. `test set map` and `test fast string map` are similar in performance, with FastStringMap performing slightly better. 2. `obj` is significantly slower than both Maps and FastStringMap due to its simple object literal nature. **Implications** The results suggest that FastStringMap performs better than JavaScript's built-in Map object in certain scenarios, particularly when it comes to set and get operations. However, the performance difference is not dramatic, and the custom implementation may come with additional overhead, such as memory allocation and deallocation. It's worth noting that these results are specific to this particular benchmark and may not generalize to all use cases or browsers. Additionally, the performance of FastStringMap can be optimized further by tweaking its internal implementation, which is not shown in the provided code. I hope this explanation helps you understand the benchmark and its results!
Related benchmarks:
string-hashcode
string-hashcode3
FastStringMap test1
FastStringMap test3
Comments
Confirm delete:
Do you really want to delete benchmark?