JS Filter Test large array
Date tested:
one year ago
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36
Test name
Executions per second
array.find
1126067.5 Ops/sec
array.filter
1188864.5 Ops/sec
lodash.filter
1101500.5 Ops/sec
Benchmark definition (click to collapse):
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 < 100; ++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);
Open this result on MeasureThat.net