Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
comparing Map and object
(version: 0)
comparing Map and object setting and getting
Comparing performance of:
setting object vs setting Map vs getting object vs getting Map
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var o = {}, m = new Map(); function randomInt(min, max){ return Math.floor(Math.random() * (max - min + 1)) + min; } function randomString(length){ var result = ''; var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; var charactersLength = characters.length; for ( var i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } var keys = []; for(var i=0; i<20; i++){ keys.push(randomString(randomInt(2, 10))); } var count = 1000000
Tests:
setting object
for(var i=0; i<count; i++){ let i = randomInt(0, 20); o[keys[i]] = i; }
setting Map
for(var i=0; i<count; i++){ let i = randomInt(0, 20); m.set(keys[i], i); }
getting object
for(var i=0; i<count; i++){ let i = randomInt(0, 20); o[keys[i]]; }
getting Map
for(var i=0; i<count; i++){ let i = randomInt(0, 20); m.get(keys[i]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
setting object
setting Map
getting object
getting Map
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 break down the benchmark and its options. **Benchmark Purpose** The benchmark compares the performance of JavaScript objects (specifically, plain objects) with JavaScript Maps in terms of setting and getting values. **Options Compared** There are four test cases: 1. **Setting Object**: The script sets a random key-value pair in an object. 2. **Setting Map**: The script sets a random key-value pair in a Map. 3. **Getting Object**: The script retrieves a random value from the same object used in the setting case. 4. **Getting Map**: The script retrieves a random value from the same Map used in the setting case. **Pros and Cons of Each Approach** 1. **Setting Object**: * Pros: Simpler to implement, less memory usage compared to Maps (since it only stores references to values). * Cons: Less efficient for large datasets or when looking up values by key. 2. **Setting Map**: * Pros: More efficient than objects for large datasets or when looking up values by key, since Maps use a hash table data structure. * Cons: Requires more memory due to the additional overhead of storing keys and values in a separate data structure. 3. **Getting Object**: Similar performance characteristics as setting an object, but with some overhead due to retrieving the value. 4. **Getting Map**: * Pros: More efficient than objects for large datasets or when looking up values by key. * Cons: May incur additional overhead due to the hash table lookup. **Library and Special JS Features** In this benchmark, no external libraries are used. However, JavaScript Maps rely on the `Set` object and the `Map` constructor, which were introduced in ECMAScript 2015 (ES6). **Considerations** * The use of random strings as keys adds additional overhead due to string lookup and comparison. * The large number of iterations (1 million) amplifies any performance differences between objects and Maps. **Alternatives** Other data structures that could be compared in a benchmark include: * JavaScript Sets: Similar to Maps, but with fewer features and less overhead. * JavaScript Arrays: Less suitable for key-value lookups than objects or Maps, but might excel in other scenarios (e.g., random access). * Other caching mechanisms, like `WeakMap` or third-party libraries. Keep in mind that the choice of data structure depends on the specific use case and requirements. This benchmark focuses on comparing the performance of objects with Maps for setting and getting values.
Related benchmarks:
Object vs Map
create temp object vs Object.fromEntries
Object.create(null) vs {} vs Map() key access (heavy)
Mappers
Comments
Confirm delete:
Do you really want to delete benchmark?