<script src='https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js'></script>
var data = _.range(10000).map(function(i) {
return {
name : name+i,
id: i
}
});
var result = _.pluck(data,'id');
var result = data.map(item => item.id);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
underscore | |
native |
Test name | Executions per second |
---|---|
underscore | 11035.6 Ops/sec |
native | 13089.5 Ops/sec |
Let's break down the provided JSON and explain what's being tested.
Benchmark Definition
The benchmark definition defines two test cases:
_
) to achieve a specific task versus using native JavaScript array methods.var result = _.pluck(data,'id');
: Using Underscore.js's pluck
function to extract a specific property from an array of objects.var result = data.map(item => item.id);
: Using native JavaScript's map
method to achieve the same result.Options Compared
The two options being compared are:
_
): A popular JavaScript library that provides functional programming utilities, including array manipulation methods.map
, pluck
, etc.Pros and Cons
Underscore.js
Pros:
Cons:
Native JavaScript Array Methods
Pros:
Cons:
Library: Underscore.js
Underscore.js is a popular JavaScript utility library that provides a set of functional programming tools, including array manipulation methods. The pluck
function used in the benchmark definition is one such method, which extracts a specific property from an array of objects.
Special JS Feature/Syntax
There's no special JavaScript feature or syntax being tested here. Both options use standard JavaScript features: Underscore.js's array methods and native JavaScript array methods.
Other Alternatives
If you're interested in exploring alternative approaches for similar tasks, consider:
find
, filter
, and reduce
which can be used for similar operations.Keep in mind that the choice of approach depends on your project's requirements, team expertise, and personal preferences.