<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var obj = {
a: 1,
b: 2,
c: 3,
}
_.forOwn(obj, (a) => {console.log(a)});
Object.keys(obj).forEach((a) => {console.log(a)});
for (let key in obj) {
// check if the property/key is defined in the object itself, not in parent
if (obj.hasOwnProperty(key)) {
console.log(key, obj[key]);
}
}
for (const [key, value] of Object.entries(obj)) {
console.log(key, value);
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash forOwn | |
Keys and then forEach | |
for key | |
for key value |
Test name | Executions per second |
---|---|
lodash forOwn | 56064.3 Ops/sec |
Keys and then forEach | 72947.8 Ops/sec |
for key | 63480.6 Ops/sec |
for key value | 65181.1 Ops/sec |
I'll take a deep breath and dive into explaining the benchmark.
Overview
The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The test compares different approaches to iterate over an object's keys in modern JavaScript browsers. We'll break down each individual test case, explain what's being tested, pros and cons of each approach, and discuss other considerations.
Test Cases
forOwn
on an object obj
. This method iterates over the object's own enumerable properties and executes a provided callback function.Benchmark Definition: _.forOwn(obj, (a) => { console.log(a) });
This test case measures how fast Lodash's forOwn
method is compared to using native JavaScript's Object.keys()
and forEach()
methods. The Object.keys()
method returns an array of the object's own enumerable property names, which can then be iterated over using forEach()
. Both approaches are commonly used for iterating over object properties in modern JavaScript.
Pros and Cons:
forOwn
: Pros - concise and readable; Cons - adds a library dependency, which may incur overhead.Object.keys()
method to get an array of property names, followed by iterating over that array using forEach()
. This approach is similar to the previous one but uses a more explicit, step-by-step approach.Benchmark Definition: Object.keys(obj).forEach((a) => { console.log(a) });
Pros and Cons:
for...in
loop with obj.hasOwnProperty(key)
to check if each property is present in the object itself, not in its prototype chain. This approach can be slower due to the overhead of checking for property existence.Benchmark Definition: for (let key in obj) { if (obj.hasOwnProperty(key)) { console.log(key, obj[key]); } }
Pros and Cons:
Object.entries()
method to get an array of key-value pairs, which can then be iterated over using a simple for...of
loop. This approach is concise and readable but might incur additional overhead compared to other methods.Benchmark Definition: for (const [key, value] of Object.entries(obj)) { console.log(key, value); }
Pros and Cons:
Libraries and Special Features
The benchmark uses Lodash's forOwn
method, which is a utility function that allows iterating over an object's properties while providing additional features like property existence checks. The other test cases do not use any external libraries or special JavaScript features beyond what's available in modern browsers.
Other Considerations
When writing performance benchmarks, it's essential to consider the following:
Alternatives
If you want to explore alternative approaches, consider the following:
Object.keys()
, forEach()
, for...in
, for...of
forOwn
method or other utility libraries like jQuery