var obj = {
$names : ['a','b','c'],
a:1,
b:2,
c:3
}
var ks = Object.getOwnPropertyNames(obj);
var n = 0;
for (var i = 0; i < ks.length; i++) {
var k = ks[i];
if (k.charAt(0) == '$') continue;
n++;
}
var ks = obj['$names'];
var n = 0;
for (var i = 0; i < ks.length; i++) {
var k = obj[ks[i]];
n++;
}
var n = 0;
for (var k in obj) {
if (k.charAt(0) == '$') continue;
n++;
}
var ks = Object.keys(obj);
var n = 0;
for (var i = 0; i < ks.length; i++) {
var k = ks[i];
if (k.charAt(0) == '$') continue;
n++;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
getOwnPropertyNames | |
Cached property names | |
for in | |
Object.keys |
Test name | Executions per second |
---|---|
getOwnPropertyNames | 7296244.5 Ops/sec |
Cached property names | 24212460.0 Ops/sec |
for in | 16048782.0 Ops/sec |
Object.keys | 8852489.0 Ops/sec |
Let's break down the provided JSON and explain what's being tested, the options compared, their pros and cons, and other considerations.
Benchmark Definition
The benchmark is defined as: "getOwnPropertyNames vs loop vs cached property names"
This benchmark aims to compare the performance of three different approaches:
getOwnPropertyNames
(Object's built-in method)$
prefix'$names'
) as an alternativeOptions Compared
Here are the options being compared, along with their pros and cons:
getOwnPropertyNames
Object
APIks
array and filters out properties with a $
prefix.'$names'
)'$names'
) to filter out unwanted properties.$names
arrayLibrary Usage
The benchmark uses the Object
API to access the getOwnPropertyNames
method. This is a built-in JavaScript library that provides various methods for manipulating objects.
Special JS Feature/Syntax
There are no special JavaScript features or syntax used in this benchmark. The code only relies on standard JavaScript constructs and APIs.
Other Considerations
When running benchmarks, it's essential to consider the following factors:
Alternatives
If you're interested in exploring alternative approaches, consider the following:
Object.keys()
or for...in
.Keep in mind that these alternatives may have their own trade-offs and potential optimizations opportunities.