Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Objects vs Maps string test
(version: 0)
Comparing performance of:
Object Insertion vs Map insertion vs Object Insertion Overwrites vs Map insertion overwrites vs Object simulated usage vs Map simulated usage
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; var iterations = 1000000; var tenthOfIterations = iterations / 10; var strings = []; for (let i = 0; i < iterations; i++) { strings.push((Math.random() + 1).toString(36).substring(2)); }
Tests:
Object Insertion
for (let i = 0; i < iterations; i++) { obj[strings[i]] = strings[i]; }
Map insertion
for (let i = 0; i < iterations; i++) { map.set(strings[i], strings[i]); }
Object Insertion Overwrites
for (let i = 0; i < iterations; i++) { obj[strings[i % 10]] = strings[i]; }
Map insertion overwrites
for (let i = 0; i < iterations; i++) { map.set(strings[i % 10], strings[i]); }
Object simulated usage
for (let i = 0; i < iterations; i++) { obj[strings[i]] = strings[i]; } for (let i = 0; i < iterations; i++) { obj[strings[i % 10]] = obj[strings[i]]; } for (let i = 0; i < tenthOfIterations; i++) { delete obj[strings[i * 10]]; }
Map simulated usage
for (let i = 0; i < iterations; i++) { map.set(strings[i], strings[i]); } for (let i = 0; i < iterations; i++) { map.set(strings[i % 10], map.get(strings[i])); } for (let i = 0; i < tenthOfIterations; i++) { map.delete(strings[i * 10]); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Object Insertion
Map insertion
Object Insertion Overwrites
Map insertion overwrites
Object simulated usage
Map simulated usage
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 difference between using objects and maps for storing string data in JavaScript. The benchmark creates an array of random strings, then inserts these strings into either an object or a map, repeating this process multiple times to simulate usage. **Options Compared** Two primary options are compared: 1. **Objects**: Using objects to store and retrieve strings. 2. **Maps**: Using maps to store and retrieve strings. **Pros and Cons of Each Approach** ### Objects Pros: * Familiarity: Many developers are more comfortable working with objects in JavaScript, as they're often used to represent complex data structures. * Easier to implement simple key-value pairs: Object literals are a concise way to create object properties. Cons: * Slow performance: Iterating over an object's properties can be slower than iterating over a map's entries. * Hash collisions: When using objects for storing strings, there's a risk of hash collisions, which can lead to slower lookup times. ### Maps Pros: * Fast performance: Looking up values in a map is typically faster than looking up keys in an object, especially when dealing with large datasets. * No hash collisions: Maps use a different data structure that avoids the problem of hash collisions. Cons: * Less familiar: Some developers might find working with maps less intuitive than objects. * Additional overhead for iteration: When iterating over a map's entries, there's additional overhead compared to object iteration. **Library and Special Features** None mentioned in the provided benchmark definition. However, it's worth noting that JavaScript engines like V8 (used by Chrome) have various features and optimizations that can affect performance, such as: * **Strict mode**: Disables certain "relaxed" syntax features, which can help prevent errors but might also impact performance. * **Symbol properties**: Introduced in ECMAScript 2015, symbols provide a way to store values with unique identifiers, which can improve performance in some cases. **Other Alternatives** If you're interested in exploring other data structures or approaches for storing and retrieving strings, consider the following: 1. **Sets**: Similar to maps but without the ordering aspect. 2. **WeakMaps**: A type of map that allows garbage collection to remove entries when they're no longer referenced. 3. **Typed arrays**: Data structures like `Uint8Array` or `Float64Array`, which can be used for storing and manipulating binary data. Keep in mind that these alternatives might not offer significant performance benefits over objects or maps, especially for simple use cases. However, exploring different options can help you better understand your JavaScript engine's strengths and weaknesses.
Related benchmarks:
Large Map vs Object 2
Map vs Object with Number Keys
Map vs object copying
Map copy vs irect copy
Map -> Array
Comments
Confirm delete:
Do you really want to delete benchmark?