Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in versus object.keys with prototype
(version: 0)
Comparing performance of:
for-in vs Object.keys
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function Foo(){} Foo.prototype.aa = '1'; Foo.prototype.bb = '1'; Foo.prototype.cc = '1'; Foo.prototype.dd = '1'; Foo.prototype.ee = '1'; Foo.prototype.ff = '1'; Foo.prototype.gg = '1'; var bar = new Foo(); bar.a = 1; bar.b = 1; bar.c = 1; bar.d = 1; bar.e = 1; bar.f = 1; bar.g = 1;
Tests:
for-in
for (var i=10000; i > 0; i--) { for (var key in bar) { console.log(key); } }
Object.keys
for (var i=10000; i > 0; i--) { Object.keys(bar).forEach(key => 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:
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):
**Overview of the Benchmark** The provided benchmark measures the performance difference between using `for-in` and `Object.keys()` to iterate over an object in JavaScript. **Benchmark Definition JSON Explanation** The Benchmark Definition JSON contains information about the test case: * `Name`: The name of the benchmark, which is "for-in versus object.keys with prototype". * `Description`: An empty string indicating that no description is provided for this benchmark. * `Script Preparation Code`: A JavaScript code snippet that creates a new instance of a custom class `Foo` and sets up its properties. This code is executed before running the test case. * `Html Preparation Code`: An empty string indicating that no HTML preparation code is required. The script preparation code creates an object `bar` with multiple properties (`aa`, `bb`, ..., `gg`) initialized with the value `'1'`. The purpose of this setup is to provide a concrete example for the test case, allowing users to benchmark the performance difference between `for-in` and `Object.keys()`. **Options Compared** Two options are compared in the benchmark: 1. **For-in**: Uses the `for-in` loop to iterate over the object's properties. 2. **Object.keys()**: Uses the `Object.keys()` method to get an array of a given object's own enumerable property names, and then uses `forEach` to iterate over this array. **Pros and Cons** * **For-in**: + Pros: Can handle objects with inherited properties and can be used in situations where you need to access both the property name and its value (via the `in` keyword). + Cons: Iterates over all properties, including non-enumerable ones, which may not be desirable in some cases. * **Object.keys()**: + Pros: Provides a more predictable and controlled iteration experience, as it only includes enumerable properties. It's also a more modern approach. + Cons: May require additional setup to include non-enumerable properties, and can lead to slower performance for very large objects. **Other Considerations** The benchmark uses the Firefox 80 browser on a Mac OS X 10.15 desktop device to ensure consistent results across different platforms. However, it's essential to note that this benchmark might not be representative of other browsers or environments. **Library and Special JS Features Used** * The `Foo` class is a custom class created for this benchmark, but its implementation does not affect the performance comparison between `for-in` and `Object.keys()`. * No special JavaScript features are used in this benchmark. **Alternatives to For-in and Object.keys() Other ways to iterate over an object's properties include: * **Array.prototype.forEach()**: Iterates over all enumerable properties of an object, but requires converting the object to an array first. * **for...in with a loop**: Similar to `for-in`, but allows for manual iteration control. * **Reflect.ownKeys()`: A more modern and efficient method for getting an array of an object's own property names. Note that these alternatives may have different performance characteristics compared to the options tested in this benchmark.
Related benchmarks:
Object vs toString
Object.prototype.hasOwnProperty vs obj.hasOwnProperty
javascript new vs Object.create 2
in vs Object.hasOwn vs Object.prototype.hasOwnProperty
Comments
Confirm delete:
Do you really want to delete benchmark?