Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values for, forEach vs Object for-in overhead cost
(version: 2)
Comparing performance of:
Object.values for vs Object.values forEach vs Object for-in
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var myObj = {}; for (var i = 0; i < 1000; i++) { myObj['val' + i] = 'val' + i; }
Tests:
Object.values for
var objValues = Object.values(myObj); var myStr = ''; for (var i = 0; i < objValues.length; i++) { myStr += objValues[i]; }
Object.values forEach
var myStr = ''; Object.values(myObj).forEach(el => myStr += el);
Object for-in
var myStr = ''; for (const o in myObj) { myStr += myObj[o]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.values for
Object.values forEach
Object for-in
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; rv:145.0) Gecko/20100101 Firefox/145.0
Browser/OS:
Firefox 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.values for
173516.9 Ops/sec
Object.values forEach
142829.6 Ops/sec
Object for-in
78636.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined is focused on analyzing the performance overhead of different methods used to iterate over the properties of a JavaScript object. Specifically, it compares three approaches: 1. **Using `Object.values()` with a `for` loop**: - **Test Case**: ```javascript var objValues = Object.values(myObj); var myStr = ''; for (var i = 0; i < objValues.length; i++) { myStr += objValues[i]; } ``` - **Pros**: - `Object.values()` returns an array of the object's values, which can be iterated over using a standard `for` loop. - This approach is typically fast, as it allows direct access to the array indices. - **Cons**: - The initial call to `Object.values()` creates a new array, which incurs a performance and memory overhead, especially with large objects. 2. **Using `Object.values()` with `forEach`**: - **Test Case**: ```javascript var myStr = ''; Object.values(myObj).forEach(el => myStr += el); ``` - **Pros**: - This syntax is quite succinct and leverages the functional style of JavaScript, making it easier to read and understand. - **Cons**: - It can be slower than a plain `for` loop. The `forEach` method involves a function call for each element, which adds overhead compared to the iterative approach. 3. **Using a `for-in` loop**: - **Test Case**: ```javascript var myStr = ''; for (const o in myObj) { myStr += myObj[o]; } ``` - **Pros**: - The `for-in` loop directly iterates over the object's enumerable properties without requiring additional data structures, which can save on memory. - **Cons**: - This approach may include inherited properties if the object's prototype has any enumerable properties, unless filtered out with `hasOwnProperty`. - Performance can vary across browsers and environments; it is generally slower than standard loops because of the way property enumeration works. ### Benchmark Results Analysis The test results indicate how many times each method was executed per second (ExecutionsPerSecond): - **Object.values for**: 73,408.38 - **Object.values forEach**: 49,388.87 - **Object for-in**: 30,124.44 From the results, we can infer that using `Object.values()` in combination with a traditional `for` loop is the fastest method, followed by `Object.values()` with `forEach`, and then the `for-in` loop. ### Other Considerations - Performance can vary significantly depending on the JavaScript engine and the complexity and size of the objects being traversed. - In modern JavaScript, where performance is crucial, the choice of iteration method can significantly impact performance-sensitive applications. - Alternatives such as `Object.entries()` (which provides both keys and values) or using modern iteration methods (e.g., `for...of` with `Object.values()`) could be explored, though those approaches may also have their own performance implications based on how they manipulate or access data. In conclusion, developers should choose an iteration strategy based on the specific use case, performance requirements, and code readability. Optimizations should always be informed by profiling data relevant to their applications.
Related benchmarks:
Object.values overhead cost
Object.values overhead cost 4
for-in vs object.keys loop test
Object.values overhead cost 1
Object.values overhead cost1
foreach: array vs object keys vs object values vs object entries
foreach: object keys vs object values vs object entries
foreach (keys vs values vs entries) vs for...in (simple vs hasOwnProperty vs hasOwn)
Object.values foreach vs Object for-in overhead cost
Comments
Confirm delete:
Do you really want to delete benchmark?