Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Traversing objects
(version: 1)
Comparing performance of:
for...in vs Object.values + forEach
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var result = {}; for (var i = 0; i < 100 ; i++) { result['key' + i] = i; } function createResult(i) { return i * i; }
Tests:
for...in
for(let key in result) { createResult(result[key]); }
Object.values + forEach
Object.values(result).forEach((value) => { createResult(value); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for...in
Object.values + forEach
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 explain what's being tested. **Benchmark Overview** The benchmark measures the performance of two approaches for traversing objects in JavaScript: 1. Using the `for...in` loop to iterate over object properties. 2. Using the `Object.values()` method and `forEach()` function to iterate over an array of values. **Script Preparation Code** The script preparation code is a simple JavaScript snippet that creates a large object with 100 properties, where each property has a unique value. The code then defines a helper function `createResult(i)` that takes a value and returns its square. ```javascript for (var i = 0; i < 100; i++) { result['key' + i] = i; } function createResult(i) { return i * i; } ``` This code sets up the object `result` with 100 properties, and defines a helper function that will be used in both benchmarking approaches. **Html Preparation Code** The html preparation code is empty, which means that no HTML is being rendered or manipulated by this test case. This suggests that the performance difference between the two approaches is purely related to JavaScript execution time. **Benchmark Comparison** The benchmark compares the performance of two approaches: 1. `for...in`: uses the `for...in` loop to iterate over object properties. 2. `Object.values + forEach`: uses the `Object.values()` method and `forEach()` function to iterate over an array of values. **Pros and Cons of Each Approach** * **For...in** + Pros: simple, efficient (no need for extra data structures), easy to understand. + Cons: can be slow due to the iteration overhead, may not work well with arrays or objects that have non-enumerable properties. * **Object.values + forEach** + Pros: more modern and efficient way of iterating over arrays, works well with most types of data. + Cons: requires extra memory allocation for the `result` array, may be slower due to the overhead of creating an array. **Library/Function Used** The `createResult()` function is a simple helper function that takes a value and returns its square. There are no external libraries or frameworks involved in this benchmark. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark, other than the use of modern `Object.values()` method and `forEach()` function. **Alternatives** Other alternatives for traversing objects might include: * Using a library like Lodash or Ramda to provide more efficient and expressive iteration functions. * Using a map-based approach with `Array.prototype.map()` instead of iterating over properties. * Using a custom iterator implementation using a `for...of` loop. Overall, the benchmark highlights the trade-offs between simple, old-school approaches (e.g. `for...in`) and more modern, efficient ones (e.g. `Object.values + forEach`).
Related benchmarks:
Javascript iterate object keys
for in / object.keys test
for in Object.keys vs foreach Object.keys
Object.entries vs Generator
For in vs Object.entries 2
Comments
Confirm delete:
Do you really want to delete benchmark?