<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>
<script>
const reduce = Function.bind.call(Function.call, Array.prototype.reduce);
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) {
return reduce(keys(O), (v, k) => concat(v, typeof k === 'string' && isEnumerable(O, k) ? [O[k]] : []), []);
};
}
</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 | 5316624.0 Ops/sec |
Object values | 23070326.0 Ops/sec |
proposal-object-values-entries/polyfill.js | 1504251.8 Ops/sec |
Benchmark Explanation
The provided JSON represents a JavaScript microbenchmark test case on the MeasureThat.net website. The benchmark compares the execution performance of three different approaches to access object values:
values
function to retrieve the object values.Object.valuess
and Object.entries
methods, which are not yet supported in all browsers.Options Compared
The benchmark compares the execution performance of these three approaches on different JavaScript engines. The options being compared are:
values
function.Object.valuess
and Object.entries
methods.Pros and Cons of Different Approaches
Library - Lodash
Lodash is a popular JavaScript library that provides a set of high-order functions and utility methods. The values
function in this benchmark is used to retrieve an array of values from an object. Lodash's values
function leverages the Object.values
method, which was introduced in ECMAScript 2017.
Special JS Feature - Spread Operator (Rest Parameter)
The spread operator (...
) is a syntax feature introduced in ECMAScript 2015. It allows for creating new arrays or objects by spreading existing ones. In this benchmark, the Function.bind.call(Function.call, Array.prototype.reduce)
syntax uses a rest parameter to handle variable numbers of arguments. This is not related to the main comparison but demonstrates advanced JavaScript features.
Alternatives
Other alternatives for accessing object values include:
for
loop.These alternatives might be useful depending on the specific use case, but they may not offer the same performance benefits as native methods like Object.values(a)
.