Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
maps vs objects - filters
(version: 0)
Comparing performance of:
for-in vs Object.values
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 'a': { id: 'a', num: 1 }, 'b': { id: 'b', num: 1 }, 'c': { id: 'c', num: 1 }, 'd': { id: 'd', num: 1 }, 'e': { id: 'e', num: 1 }, 'f': { id: 'f', num: 1 }, 'g': { id: 'g', num: 1 }, }; var objmap = new Map(Object.entries(obj)) function filter(value) { return value.id === 'c' }
Tests:
for-in
for (var i=10000; i > 0; i--) { Array.from(objmap.values()).filter(filter); }
Object.values
for (var i=10000; i > 0; i--) { Object.values(obj).filter(filter); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for-in
Object.values
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
512.0 Ops/sec
Object.values
421.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark that compares two approaches: using an object's `values()` method and using a `Map` to iterate over its values. **What is being tested?** In this benchmark, we have a large object (`obj`) with 8 properties, each containing an `id` and a `num` value. The task is to filter out the objects where `id` is not equal to `'c'`. We compare two approaches: 1. Using `Object.values()` method: This method returns an array of values from the object. 2. Using a `Map` data structure: A `Map` is created by iterating over the object's entries using `Object.entries()`, and then filtering its values using a callback function. **Options compared** The two options being compared are: 1. **Object.values()**: This method returns an array of values from the object. 2. **Map**: Using a `Map` to iterate over the object's values, filtered by a callback function. **Pros and Cons** * **Object.values():** + Pros: Simple and straightforward, easy to understand for beginners. + Cons: May be slower due to the overhead of creating an array and iterating over it. * **Map:** + Pros: Can be faster, especially for large datasets, since it avoids creating an intermediate array and allows for more efficient iteration. + Cons: Requires understanding of `Map` data structure and may be less intuitive for beginners. **Library usage** In this benchmark, the `Map` library is used to create a `Map` instance from the object's entries using `Object.entries()`. The `filter()` method is then applied to the `Map`'s values using a callback function. **Special JavaScript feature/syntax** There are no specific JavaScript features or syntax being tested in this benchmark. However, it does use modern JavaScript features such as: * Arrow functions (`function filter(value) { ... }`) * Template literals (e.g., `"var obj = {\r\n 'a': {\r\n id: 'a',\r\n num: 1\r\n },\r\n ...`) **Other alternatives** If you were to rewrite this benchmark using alternative approaches, you might consider: * Using `for...in` loops instead of `Object.values()` or `Map`. * Using a library like Lodash or Ramda for functional programming. * Using Web Workers or parallel processing to scale the benchmark further. Keep in mind that these alternatives would require significant changes to the benchmark and may not be as efficient or scalable.
Related benchmarks:
filter vs id lookup
lodash intersectionBy vs array filtering with includes
array filtering with some vs array filtering with includes
filter vs flatMap
filter vs flatMap v2
Comments
Confirm delete:
Do you really want to delete benchmark?