var obj = {
$props : ['a','b','c'],
a:1,
b:2,
c:3
}
let n = 0;
for (let k in obj) {
n++;
}
const arr = Object.keys(obj);
let n = 0;
for (let i = 0; i < arr.length; i++) {
n++;
}
const arr = Object.getOwnPropertyNames(obj);
let n = 0;
for (let i = 0; i < arr.length; i++) {
n++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
asta castiga btw | |
meh | |
meh 2 |
Test name | Executions per second |
---|---|
asta castiga btw | 5259094.0 Ops/sec |
meh | 2147182.2 Ops/sec |
meh 2 | 2068514.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Overview
The provided benchmark compares three ways to iterate over an object in JavaScript: for...in
, Object.keys()
, and Object.getOwnPropertyNames()
. The goal is to measure which approach is fastest, most efficient, or perhaps has some other advantage.
Options Compared
We have two options compared:
for...in
: This traditional way of iterating over an object's properties uses the for...in
loop syntax and returns both enumerable and non-enumerable properties.Object.keys()
: This method returns only the enumerable properties of an object as an array, allowing for more fine-grained control over iteration.Object.getOwnPropertyNames()
: Similar to Object.keys()
, but also includes non-enumerable properties in the result.Pros and Cons
Here's a brief rundown of each approach:
for...in
:Object.keys()
:for...in
.Object.getOwnPropertyNames()
:Object.keys()
when iterating over a large object with many non-enumerable properties.When deciding between these options, consider the specific requirements of your project:
Object.keys()
.for...in
for its simplicity and familiarity.Object.getOwnPropertyNames()
when you need to ensure all properties are processed.Library Usage
None of the provided benchmark definitions rely on any external libraries. The only library-like functionality is inherent to the JavaScript language itself, such as Object.keys()
, Object.getOwnPropertyNames()
, and the for...in
loop.
Special JS Features or Syntax
The provided benchmark examples are straightforward and do not use any advanced JavaScript features like async/await, arrow functions, or classes. The focus is on understanding how to iterate over objects using different approaches.
By understanding these three options for iterating over objects, developers can choose the best approach for their specific needs, considering factors such as performance, readability, and accuracy.
Other Alternatives
Some other alternatives for iterating over objects include:
Array.prototype.forEach()
: This method is similar to Object.keys()
, but uses an array of properties instead. It's a good alternative when you need to process all enumerable properties.for...of
loops: Introduced in ECMAScript 2015, these loops provide an even more concise and readable way to iterate over objects with specific iterables.Keep in mind that these alternatives might not be directly comparable to the provided options, but they can offer different advantages depending on your use case.