Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs for-of object.keys
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1, 'f': 1, 'g': 1 };
Tests:
for-in
for (var i=10000; i > 0; i--) { for (var key in obj) { if (!obj.hasOwnProperty(key)) { continue } console.log(key); } }
Object.keys
for (var i=10000; i > 0; i--) { for (var key of Object.keys(obj)) { console.log(key) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for-in
Object.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:131.0) Gecko/20100101 Firefox/131.0
Browser/OS:
Firefox 131 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
6.2 Ops/sec
Object.keys
6.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The provided JSON represents a benchmark definition, which is a template for creating and running JavaScript microbenchmarks. The template includes: 1. `Name`: A unique name for the benchmark. 2. `Description`: An optional description of the benchmark (in this case, null). 3. `Script Preparation Code`: A code snippet that sets up the test environment and objects used in the benchmark. 4. `Html Preparation Code`: An optional HTML code snippet that can be used to create a user interface for the benchmark (in this case, null). In this specific benchmark definition, we have two object literals with 7 properties each. **Test Cases** The benchmark consists of two test cases: 1. **for-in**: This test case uses an iterative approach to iterate over the `obj` object's properties. 2. **Object.keys**: This test case uses the built-in `Object.keys()` method to iterate over the `obj` object's properties. **Comparison of Options** The two test cases differ in their approach: 1. **for-in**: This method uses the `in` operator to check if a property exists in the object, which can lead to slower performance compared to other methods. 2. **Object.keys**: This method uses the `Object.keys()` function, which is generally faster and more efficient than using `in`. **Pros and Cons** Here's a summary of the pros and cons for each approach: * **for-in**: + Pros: Can be used with arrays and objects. + Cons: May lead to slower performance due to property existence checks. * **Object.keys**: + Pros: Generally faster and more efficient than using `in`. + Cons: Limited to iterating over object properties. **Library Used** In this benchmark, the built-in `Object.keys()` method is used, which is a part of the JavaScript language itself. This means that the performance difference between the two test cases is largely due to the specific implementation and optimization of the `Object.keys()` function in each browser version. **Special JS Feature or Syntax** There are no special features or syntaxes being tested in this benchmark. The code snippets provided are standard JavaScript code, without any experimental or proposal features. **Other Alternatives** If you'd like to explore alternative approaches, here are a few options: 1. **for...of**: This method uses an iterative approach similar to `Object.keys()`, but with a different syntax. 2. **forEach**: This method can be used to iterate over the object's properties, but it may not provide the same level of performance as `Object.keys()`. Note that these alternatives might have their own trade-offs in terms of performance, readability, and browser compatibility.
Related benchmarks:
For in vs For of
function+for-in vs Object.keys
Object.keys(obj)[0] vs for in
Object.entries vs Object.keys vs for...in
for-in vs for object.keys keys
Comments
Confirm delete:
Do you really want to delete benchmark?