<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var array = [
{ 'name': 'lim', 'age': 26 },
{ 'name': 'kim', 'age': 28 },
{ 'name': 'choi', 'age': 32 },
{ 'name': 'park', 'age': 21 }
];
_.filter(array, arr => arr.age > 26)
array.filter(arr => arr.age > 26)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash filter method | |
es6 filter method |
Test name | Executions per second |
---|---|
lodash filter method | 6618839.0 Ops/sec |
es6 filter method | 18669010.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to compare the performance of two approaches: using the Lodash library's filter
method versus the native JavaScript array filter
method in ES6.
Options Being Compared
Two options are being compared:
filter
function that can be used to filter arrays.filter
, introduced in ES6, to filter arrays.Pros and Cons of Each Approach
Library: Lodash
Lodash is a popular utility library that provides a wide range of functions for working with arrays, objects, and other data structures. The filter
function is one of its most commonly used methods. By using Lodash's filter
method, the benchmark takes advantage of the library's optimized implementation and avoids having to implement filtering logic manually.
Special JS Feature/ Syntax: ES6
The benchmark uses ES6 syntax for defining the array filter method, which includes features like arrow functions (arr => arr.age > 26
) and template literals ('\r\n { 'name': 'lim', 'age': 26 }\r\n'
). This is a newer syntax introduced in ES6 that allows for more concise and expressive code.
Other Alternatives
If you wanted to modify or extend this benchmark, here are some alternative approaches you could consider:
filter
method for each one.Overall, this benchmark provides a simple and effective way to compare the performance of two approaches: using Lodash's filter
method versus the native JavaScript array filter
method in ES6.