<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>
<script>
const forEach = Function.bind.call(Function.call, Array.prototype.forEach);
const isEnumerable = Function.bind.call(Function.call, Object.prototype.propertyIsEnumerable);
const concat = Function.bind.call(Function.call, Array.prototype.concat);
const keys = Reflect.ownKeys;
if (!Object.valuess) {
Object.valuess = function values(O) {
const values = [];
for(val in O) values.push(val);
return values;
};
}
</script>
var a = { a: 1, b: 2, c: 3};
_.values(a);
Object.values(a);
Object.valuess(a);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.values | |
Object values | |
proposal-object-values-entries/polyfill.js |
Test name | Executions per second |
---|---|
_.values | 2336495.2 Ops/sec |
Object values | 10145503.0 Ops/sec |
proposal-object-values-entries/polyfill.js | 20998538.0 Ops/sec |
Overview of the Benchmark
The provided benchmark measures the performance of three different approaches to iterate over object values:
_.values(a)
(using Lodash)Object.values(a)
Object.valuess(a)
(a polyfill for Object.values)Lodash Approach (_.values(a)
)
_
is an alias for the Lodash namespace.values
function returns a new array containing the values of the given object.Pros:
Cons:
Native Approach (Object.values(a)
)
Pros:
Cons:
Polyfill Approach (Object.valuess(a)
)
Pros:
Cons:
Comparison
The benchmark measures the execution speed of each approach, and the results indicate that:
Object.values(a)
approach is the fastest._values(a)
approach is slightly slower than the native approach.Object.valuess(a)
approach is the slowest.Other Alternatives
For iterating over object values, other approaches include:
for (var key in a) { ... }
Array.prototype.forEach.call(Object.keys(a), function(key) { ... });
However, these approaches are generally slower than the native Object.values(a)
method and may have additional overhead due to the use of loops or array operations.
It's worth noting that the benchmark's focus is on measuring performance for this specific use case, and other factors like memory usage, cache behavior, or thread safety may be relevant in different scenarios.