Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values foreach vs Object for-in overhead cost
(version: 1)
Comparing performance of:
Call Object.values on each execution vs Use shared Object.values
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var myObj = {}; for (var i = 0; i < 1000; i++) { myObj['val' + i] = 'val' + i; } //var myObjValues = Object.values(myObj);
Tests:
Call Object.values on each execution
var objValues = Object.values(myObj); var myStr = ''; objValues.forEach(el => myStr += el);
Use shared Object.values
var myStr = ''; for (const o in myObj) { myStr += myObj[o]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Call Object.values on each execution
Use shared 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; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Call Object.values on each execution
73970.2 Ops/sec
Use shared Object.values
41563.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark "Object.values foreach vs Object for-in overhead cost" aims to compare the performance of two approaches for accessing object properties in JavaScript: using `Object.values()` combined with `forEach()` versus using a `for...in` loop. ### Options Compared: 1. **Using `Object.values()` and `forEach()`** - **Benchmark Definition**: ```javascript var objValues = Object.values(myObj); var myStr = ''; objValues.forEach(el => myStr += el); ``` - **Test Name**: "Call Object.values on each execution" 2. **Using `for...in` loop** - **Benchmark Definition**: ```javascript var myStr = ''; for (const o in myObj) { myStr += myObj[o]; } ``` - **Test Name**: "Use shared Object.values" ### Pros and Cons of Each Approach: 1. **Using `Object.values()` and `forEach()`** - **Pros**: - `Object.values()` provides a clear and semantic way to retrieve all values of an object, which can be more readable than a loop. - It operates directly on the values in a concise manner with `forEach()`, making it easier to handle. - **Cons**: - There is overhead associated with creating a new array (to hold the values) each time `Object.values()` is called. - Memory consumption may increase when dealing with large objects since a new array is generated. 2. **Using `for...in` loop** - **Pros**: - More memory-efficient because it directly iterates over the properties of the object without the need for intermediate storage (no new array is created). - Suitable for iterating over all enumerable properties in an object without incurring the cost of generating an additional array. - **Cons**: - The syntax can be slightly less clear and less functional than using `Object.values()`. - Developers must ensure to use `hasOwnProperty` to avoid iterating over inherited properties, which can introduce bugs if not handled properly. ### Other Considerations: - The overall performance results from the benchmark indicate that `Object.values()` with `forEach()` executes approximately 1.78 times faster than the `for...in` loop on the tested browser (Firefox 138, Windows platform). - Performance could vary based on the JavaScript engine and the number of properties being processed. ### An Insight into the Libraries and Syntax: - No specific external library is utilized in this benchmark; the approaches rely purely on native JavaScript methods and syntax. - The `Object.values()` method is a standard ES2017 (ES8) feature that returns an array of a given object's own enumerable property values. This is a straightforward way to work with object values, particularly in modern JavaScript programming. ### Alternatives: - **`Object.entries()`**: Instead of retrieving values, this method can return both keys and values as pairs, which might be beneficial if you also need access to the keys during iteration. - **`Array.prototype.map()` with `Object.keys()`**: Another alternative would be to use `map()` with the keys of the object, but like `Object.values()`, this would also incur array creation overhead. The choice between these alternatives mainly depends on the specific needs of the application, such as readability versus performance and memory usage.
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 for, forEach vs Object for-in overhead cost
Comments
Confirm delete:
Do you really want to delete benchmark?