Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Inverted map lookup vs. dynamic map lookup vs. Set1
(version: 0)
Comparing performance of:
Inverted map lookup vs Dynamic lookup vs Set
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const testMap = { test0: '000', test1: '001', test2: '002', test3: '003', test4: '004', test5: '005', test6: '006', test7: '007', test8: '008', test9: '009' }; let setA = new Set(); setA.add(000); setA.add(001); setA.add(002); setA.add(003); setA.add(004); setA.add(005); setA.add(006); setA.add(007); setA.add(008); setA.add(009); // tiny in-place inverting function using array reduce const testMapInverted = Object.keys(testMap).reduce((acc, val) => { acc[testMap[val]] = val; return acc; }, {}); function numberStringInvertedMapLookup(numberString) { return testMapInverted.hasOwnProperty(numberString); } function numberStringDynamicLookup(numberString) { return Object.values(testMap).includes(numberString); } function numberStringSet(numberString) { return setA.has(numberString); } function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); }
Tests:
Inverted map lookup
for(let i = 0; i < 10000; ++i) { numberStringInvertedMapLookup('00' + getRandomInt(20)); }
Dynamic lookup
for(let i = 0; i < 10000; ++i) { numberStringDynamicLookup('00' + getRandomInt(20)); }
Set
for(let i = 0; i < 10000; ++i) { numberStringSet('00' + getRandomInt(20)); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Inverted map lookup
Dynamic lookup
Set
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):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The provided benchmark measures the performance of three different data structures: an inverted map, a dynamic map (or a regular object with `in` operator), and a Set. The test cases are designed to search for specific values within these data structures. **Options Compared** 1. **Inverted Map**: A custom-built in-place inversion function uses `Array.prototype.reduce()` to create an inverted map from the original `testMap`. This approach requires manual implementation of the lookup logic. 2. **Dynamic Lookup**: The test case uses the `in` operator to check if a value exists within the `testMap`. This is a built-in JavaScript feature that allows for efficient lookup in objects. 3. **Set**: A Set data structure is used with the `has()` method to perform the search. **Pros and Cons** * **Inverted Map**: Pros: Custom implementation, potentially faster if optimized. Cons: Requires manual implementation of the inversion function and may have higher overhead due to the extra computation. * **Dynamic Lookup**: Pros: Built-in JavaScript feature, efficient, and widely supported. Cons: May not be as fast for very large datasets due to the object lookup overhead. * **Set**: Pros: Fast, efficient, and optimized by the browser. Cons: Not suitable for all use cases (e.g., searching for specific values). **Other Considerations** * The test case uses a small dataset with only 10,000 iterations, which may not be representative of real-world scenarios. * The inverted map implementation is quite simplistic and may not cover edge cases or handle large datasets efficiently. **Library and Special JavaScript Features** The benchmark uses the following libraries: * None explicitly mentioned; however, it relies on built-in JavaScript features like `Array.prototype.reduce()`, `Object.keys()`, and `Set`. As for special JavaScript features, none are explicitly mentioned in this benchmark. However, the use of the `in` operator for dynamic lookup is a notable example. **Alternatives** If you're looking to optimize your code or improve performance: * Consider using more advanced data structures like maps (e.g., with caching or lazy loading) or trie-based data structures for efficient lookups. * Use existing JavaScript libraries or modules optimized for specific use cases, such as Set implementations with better performance characteristics. * Explore modern web performance optimization techniques and frameworks that can help improve the execution speed of your code. For a more comprehensive understanding of each benchmark case's behavior and potential optimizations: * Study the documentation and examples provided by Mozilla (for `in` operator) or explore existing benchmarks and microbenchmarking resources for JavaScript. * Implement alternative data structures, such as maps with caching or lazy loading, and compare their performance characteristics. Feel free to ask me any questions about this benchmark explanation.
Related benchmarks:
for vs map
Map from .reduce vs Map from .map
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
Array from() vs Map.keys() vs Map.values() vs spread (fixed)
Comments
Confirm delete:
Do you really want to delete benchmark?