<script src='https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js'></script>
var primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,97]
var result = _.filter(primes,(prime) => {return prime % 2 == 0});
var result = primes.filter((prime) => {return prime % 2 == 0});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Underscore filter | |
Array filter |
Test name | Executions per second |
---|---|
Underscore filter | 1990546.1 Ops/sec |
Array filter | 8709297.0 Ops/sec |
Let's break down what's being tested in the provided JSON benchmark.
Benchmark Definition
The benchmark is comparing two approaches to filtering an array of prime numbers:
filter()
method directly on an Array (Array filter
)._filter()
function from the Underscore.js library (Underscore filter
).Options compared
In this case, we're only comparing these two approaches.
Pros and Cons:
Other Considerations:
Underscore.js Library
Underscore.js is a popular JavaScript utility library created by Jeremy Ashkenas in 2009. Its main purpose is to provide functional programming utilities, making it easier to work with data and manipulate strings, arrays, and objects. The _filter()
function is one of its core functions, which applies a callback function to each element in an array and returns a new array containing only the elements for which the callback returned true
.
JavaScript Features/Syntax
There are no specific JavaScript features or syntax being tested in this benchmark.
Benchmark Preparation Code
The preparation code provided includes:
primes
) used as input for the filtering operations._filter()
function is available.Overall, this benchmark provides a clear comparison between two approaches to filtering an array in JavaScript, highlighting potential performance differences and the benefits of using optimized libraries like Underscore.js.