<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var value = {a: 30310, b: 100303, c: 3040494};
_.values(value)
Object.values(value)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Native |
Test name | Executions per second |
---|---|
Lodash | 28218566.0 Ops/sec |
Native | 67233336.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares the performance of two approaches to extract values from an object: using the Lodash library (_.values(value)
) versus the native Object.values()
method.
Options Compared
Two options are being compared:
_.values()
method to access the values of the value
object.Object.values()
method to access the values of the value
object.Pros and Cons
Here are some pros and cons of each approach:
Object.values()
method is a lightweight, built-in function that doesn't introduce any additional dependencies.Other Considerations
When choosing between these two approaches, consider the following:
Object.values()
method might not be an option.Object.values()
method is a lightweight alternative.Library (Lodash)
The Lodash library is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string processing, and object transformation. The _.values()
method is one of its many utility functions that can be used to access the values of an array-like object.
Special JS Feature or Syntax (Not Applicable)
There are no special JavaScript features or syntaxes being tested in this benchmark.
Benchmark Preparation Code
The script preparation code includes:
var value = {a: 30310, b: 100303, c: 3040494};
This sets up a simple object value
with three properties and their corresponding values. The HTML preparation code includes the script tag that loads the Lodash library:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Other Alternatives
If you're looking for alternative ways to access object values, consider using:
for...in
loop: for (var key in value) { console.log(key + ': ' + value[key]); }
Object.keys()
method: Object.keys(value).forEach(function(key) { console.log(key + ': ' + value[key]); });
However, these approaches are generally less efficient and more verbose than the native Object.values()
method or Lodash's _.values()
method.