Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys loop test
(version: 0)
Comparing performance of:
Object.keys vs for-in
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {}; for (var i=1000; i > 0; i--) { obj[""+i] = i; }
Tests:
Object.keys
var ctr = 0; for (var i=100; i > 0; i--) { Object.keys(obj).forEach(key => ctr++); } console.log(ctr);
for-in
var ctr = 0; for (var i=100; i > 0; i--) { for(var key in obj) { ctr++; } } console.log(ctr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.keys
for-in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15
Browser/OS:
Safari 16 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.keys
111.5 Ops/sec
for-in
96.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested in each benchmark. **Benchmark Overview** The `MeasureThat.net` website allows users to create and run JavaScript microbenchmarks. The test cases compare two approaches: using `Object.keys()` vs using `for-in` loops. **Script Preparation Code** The script preparation code is the same for both benchmarks: ```javascript var obj = {}; for (var i=1000; i > 0; i--) { obj[\"\"+i] = i; } ``` This code creates an object with properties named `0`, `1`, ..., `999` and assigns each property a value equal to its index. **Test Cases** There are two test cases: ### Object.keys ```javascript var ctr = 0; for (var i=100; i > 0; i--) { Object.keys(obj).forEach(key => ctr++); } console.log(ctr); ``` This benchmark uses the `Object.keys()` method to iterate over the object's properties and increments a counter for each iteration. ### for-in ```javascript var ctr = 0; for (var i=100; i > 0; i--) { for(var key in obj) { ctr++; } } console.log(ctr); ``` This benchmark uses a traditional `for-in` loop to iterate over the object's properties and increments a counter for each iteration. **Library Used** Both benchmarks use the `Object.keys()` method, which is a built-in method of the JavaScript `Object` prototype. The purpose of this method is to return an array-like object containing the property names of the specified object. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in these benchmarks beyond what's required for the test itself. **Pros and Cons** Here are some pros and cons of each approach: * **Object.keys()** + Pros: - More concise and readable code - Less error-prone than traditional `for-in` loops + Cons: - May be slower due to the overhead of creating an array-like object - May not work as expected in older browsers that don't support it * **for-in** + Pros: - May be faster due to reduced overhead compared to `Object.keys()` - Works consistently across older browsers and environments + Cons: - Less concise and more error-prone code - Can lead to issues with property iteration if not used carefully **Other Alternatives** If you need to iterate over an object's properties in a JavaScript benchmark, other alternatives include: * Using the `Array.prototype.forEach()` method directly on the object: `obj.keys().forEach(key => ctr++)` * Using a library like Lodash's `_.keys()` function * Implementing your own property iteration loop using `for...of` or `iterators` Keep in mind that the choice of approach ultimately depends on your specific use case and performance requirements.
Related benchmarks:
Object iteration for-in vs object.keys
for-in vs object keys map vs object keys loop
for-in vs object.keys vs object.values for objects perf 5
for in / object.keys test
for in loop
Comments
Confirm delete:
Do you really want to delete benchmark?