Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map() vs Object() on write and read
(version: 0)
Comparing performance of:
Map vs Object Iteration
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function filter(v) { return v % 2 }
Tests:
Map
const test = new Map() for (var i=10000; i > 0; i--) { test.set(`key${i}`, i) } for (var i=100; i > 0; i--) { Array.from(test.values()).filter(filter) }
Object Iteration
const test = {} for (var i=10000; i > 0; i--) { test[`key${i}`] = i } for (var i=100; i > 0; i--) { const values = []; for (const id in test) { const value = test[id]; if (filter(value)) { values.push(value); } } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map
Object Iteration
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map
91.1 Ops/sec
Object Iteration
10.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmarking data. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark test case, which measures the performance of two different approaches: using `Map` and `Object` data structures for iteration. **Options Compared** Two options are compared: 1. **Map**: Uses an instance of the `Map` class to store key-value pairs. The map is populated with 10,000 entries, where each entry has a unique key (in the format "keyX") and a value equal to its index (i.e., `i` in the loop). The benchmark then iterates over the values using the `filter()` method. 2. **Object Iteration**: Uses an empty object (`{}`) to store key-value pairs, where each key is also in the format "keyX" and the value is equal to its index (i.e., `i` in the loop). The benchmark then iterates over the object's properties using a `for...in` loop with `const values = []`, filtering out the values that don't meet the condition. **Pros and Cons** Here are some pros and cons of each approach: **Map** Pros: * Faster iteration: Maps provide an optimized iteration mechanism, making it generally faster than object iteration. * Memory efficiency: Maps use less memory since they only store the key-value pairs without any additional overhead for property names. Cons: * Limited control over iteration order: The map's iteration is determined by its internal algorithm, which may not provide the exact control you want over the iteration order. **Object Iteration** Pros: * More control over iteration order: You have direct access to each property name and can easily skip or iterate in a specific order. * No dependency on the map's implementation: Since it's using a standard `for...in` loop, the performance is less dependent on the map's internal implementation. Cons: * Slower iteration: Object iteration is generally slower than map iteration due to the overhead of property name lookup and potential unnecessary checks during iteration. **Other Considerations** The benchmarking process assumes that: * The filter() function is applied only to the values, not to the keys or any other part of the data structure. * There are no side effects or concurrent modifications to the data structures during the execution of the benchmark. **Library Usage (None)** There are no external libraries used in this benchmark. Both Map and Object Iteration rely on built-in JavaScript features. **Special JS Feature/Syntax (None)** Since there's no special JavaScript feature or syntax used, I won't mention anything about it. **Alternatives** Some alternative approaches to compare with Map and Object Iteration could be: * Using `Set` instead of `Map`, which has similar performance characteristics but might not provide the exact same iteration behavior. * Implementing a custom data structure using arrays or other data structures that are more memory-efficient than Maps, but might require additional manual iteration logic. * Using libraries like Lodash or Ramda to implement custom iterations and filtering, potentially offering better control over iteration order. Keep in mind that these alternatives would likely have different performance characteristics and trade-offs compared to the original Map and Object Iteration approaches.
Related benchmarks:
filter-map vs reduce
flatMap() vs filter().map() vs map().filter(Boolean)
flatMap() vs map().filter() v1
flatMap() vs map().filter() v2
Filtering and mapping with .filter(...).map(...) vs .flatMap(...) vs custom filterMap(...)
Comments
Confirm delete:
Do you really want to delete benchmark?