Test name | Executions per second |
---|---|
array.find | 605417.6 Ops/sec |
array.filter | 677150.8 Ops/sec |
lodash.filter | 529410.6 Ops/sec |
<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>
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;
}
array.find(getValue);
array.filter(getValue);
_lodash.filter(array, getValue);