<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>
function extractValues(obj) {
const t = [];
for(var key in obj) { t.push(obj[key]); }
return t;
}
var i;
var a = {};
for (i = 0; i < 1000; i++) {
a[i + ""] = i + "text";
}
Object.values(a);
var i;
var a = {};
for (i = 0; i < 1000; i++) {
a[i + ""] = i + "text";
}
_.values(a);
var i;
var a = {};
for (i = 0; i < 1000; i++) {
a[i + ""] = i + "text";
}
extractValues(a);
var i;
var a = {};
for (i = 0; i < 1000; i++) {
a[i + ""] = i + "text";
}
Object.keys(a).map(key => a[key]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.values(obj) | |
_.values(obj) | |
for...in | |
Object.keys(obj).map(key => obj[key]) |
Test name | Executions per second |
---|---|
Object.values(obj) | 21060.3 Ops/sec |
_.values(obj) | 12955.6 Ops/sec |
for...in | 11866.5 Ops/sec |
Object.keys(obj).map(key => obj[key]) | 13446.3 Ops/sec |
Let's break down the provided benchmark and its components.
Benchmark Overview
The benchmark compares the performance of three different approaches to extract values from an object with 1000 entries:
Object.values(obj)
_values(obj)
(using the Lodash library)for...in
loop (extractValues(a)
)Object.keys(obj).map(key => obj[key])
Options Compared
The benchmark compares the performance of:
Object.values()
and Object.keys().map()
_values()
from Lodashfor...in
loopPros and Cons of Each Approach
Object.values(obj)
: This is the most straightforward approach, as it's a native JavaScript method that directly returns an array of object values._values() from Lodash
: This approach uses a third-party library to provide an alternative implementation of the value extraction process.Object.values()
method.extractValues(a)
: This approach uses a manual implementation of the value extraction process using a for...in
loop.Object.values()
method and introduces additional complexity.Other Considerations
Object.keys().map()
.Library and Syntax
The benchmark uses the Lodash library to provide the _values()
function. Lodash is a popular utility library that provides a wide range of functions for common tasks, such as array manipulation, object modification, and more.
There are no special JavaScript features or syntax used in this benchmark beyond what's already discussed.