Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sherlock Benchmark - JS Filter Test large array
(version: 0)
Comparing performance of:
array.find vs array.filter vs lodash.filter
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"> </script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.29/angular.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script> <script> _lodash = _.noConflict(); </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
Script Preparation code:
var angularFilter = angular.injector(['ng']).get('$filter')('filter'); // Generate test array window.array = (function() { var arr = []; for (var i = 0; i < 50000; ++i) { arr.push(i); } return arr; }()); Array.prototype.find = function(predicate) { if (this == null) { throw new TypeError('Array.prototype.find called on null or undefined'); } if (typeof predicate !== 'function') { throw new TypeError('predicate must be a function'); } var list = Object(this); var length = list.length >>> 0; var thisArg = arguments[1]; var value; for (var i = 0; i < length; i++) { value = list[i]; if (predicate.call(thisArg, value, i, list)) { return value; } } return undefined; }; function getValue(el) { return el - 50 > 31; }
Tests:
array.find
array.find(getValue);
array.filter
array.filter(getValue);
lodash.filter
_lodash.filter(array, getValue);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
array.find
array.filter
lodash.filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript benchmark created on MeasureThat.net, which tests the performance of filtering large arrays using different approaches. **Benchmark Definition** The benchmark defines three test cases: 1. `array.find(getValue);`: This test case uses the built-in `find()` method of the Array prototype to filter the array. 2. `array.filter(getValue);`: This test case uses the built-in `filter()` method of the Array prototype to filter the array. 3. `_lodash.filter(array, getValue);`: This test case uses the Lodash library's `filter()` function to filter the array. **Options Compared** The benchmark compares the performance of these three approaches: * **Built-in methods (`array.find` and `array.filter`)**: These methods are part of the JavaScript language standard and are implemented in V8 (the engine used by Chrome). * **Lodash library (`_lodash.filter`)**: This is a popular utility library that provides additional functional programming features, including filtering functions. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Built-in methods (`array.find` and `array.filter`)**: * Pros: Native performance, easy to use. * Cons: Limited control over implementation details, may not be optimized for specific use cases. 2. **Lodash library (`_lodash.filter`)**: * Pros: Provides additional functionality beyond built-in methods, customizable implementation. * Cons: Adds overhead due to the library itself, may require additional setup. **Library and Syntax** The benchmark uses the Lodash library version 4.17.2. The `_lodash` variable is used as a no-conflict alias for the `_.noConflict()` function, which returns the original Lodash object to avoid polluting the global namespace. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in this benchmark. **Other Considerations** When choosing between these approaches, consider the following factors: * **Performance**: If native performance is critical, built-in methods might be a better choice. If customization and additional functionality are important, Lodash library might be a better fit. * **Readability and maintainability**: Built-in methods are often more concise and readable. The Lodash library provides more features, but may require more setup and maintenance. **Alternatives** Other alternatives for filtering arrays include: 1. **Regular expressions**: Using regular expressions to filter arrays can provide fine-grained control over the filtering process. 2. **Map functions**: Applying a map function to an array can be used to filter it. 3. **Custom implementation**: Writing a custom filtering function using JavaScript's core functionality can provide optimal performance and flexibility. Keep in mind that each alternative has its own trade-offs and may not offer better performance or readability than the built-in methods or Lodash library.
Related benchmarks:
Array.prototype.filter vs Lodash filter vs L
Array.prototype.filter vs Lodash filter 1Million
Array.prototype.filter vs Lodash without 3
Array.prototype.filter vs Lodash without 4
for loop filter vs native array.filter
Comments
Confirm delete:
Do you really want to delete benchmark?