mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 01:57:50 +00:00
Tweak _.find to avoid using _.identity and add benchmark.
Former-commit-id: a881f90d00828ed6af5b2b0bc80c9ae6e2cb2da8
This commit is contained in:
11
lodash.js
11
lodash.js
@@ -302,6 +302,11 @@
|
|||||||
'inLoop': 'callback(collection[index], index, collection) && result.push(collection[index])'
|
'inLoop': 'callback(collection[index], index, collection) && result.push(collection[index])'
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Reusable iterator options for `find` and `forEach` */
|
||||||
|
var forEachIteratorOptions = {
|
||||||
|
'top': 'if (thisArg) callback = bind(callback, thisArg)'
|
||||||
|
};
|
||||||
|
|
||||||
/** Reusable iterator options for `map`, `pluck`, and `values` */
|
/** Reusable iterator options for `map`, `pluck`, and `values` */
|
||||||
var mapIteratorOptions = {
|
var mapIteratorOptions = {
|
||||||
'init': '',
|
'init': '',
|
||||||
@@ -584,7 +589,7 @@
|
|||||||
* var even = _.find([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
|
* var even = _.find([1, 2, 3, 4, 5, 6], function(num) { return num % 2 == 0; });
|
||||||
* // => 2
|
* // => 2
|
||||||
*/
|
*/
|
||||||
var find = createIterator(baseIteratorOptions, {
|
var find = createIterator(baseIteratorOptions, forEachIteratorOptions, {
|
||||||
'init': '',
|
'init': '',
|
||||||
'inLoop': 'if (callback(collection[index], index, collection)) return collection[index]'
|
'inLoop': 'if (callback(collection[index], index, collection)) return collection[index]'
|
||||||
});
|
});
|
||||||
@@ -611,9 +616,7 @@
|
|||||||
* _([1, 2, 3]).forEach(function(num) { alert(num); }).join(',');
|
* _([1, 2, 3]).forEach(function(num) { alert(num); }).join(',');
|
||||||
* // => alerts each number in turn and returns '1,2,3'
|
* // => alerts each number in turn and returns '1,2,3'
|
||||||
*/
|
*/
|
||||||
var forEach = createIterator(baseIteratorOptions, {
|
var forEach = createIterator(baseIteratorOptions, forEachIteratorOptions);
|
||||||
'top': 'if (thisArg) callback = bind(callback, thisArg)'
|
|
||||||
});
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Produces a new array of values by mapping each value in the `collection`
|
* Produces a new array of values by mapping each value in the `collection`
|
||||||
|
|||||||
16
perf/perf.js
16
perf/perf.js
@@ -205,6 +205,22 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
suites.push(
|
||||||
|
Benchmark.Suite('find')
|
||||||
|
.add('Lo-Dash', function() {
|
||||||
|
lodash.find(numbers, function(num) {
|
||||||
|
return num === 19;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.add('Underscore', function() {
|
||||||
|
_.find(numbers, function(num) {
|
||||||
|
return num === 19;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
suites.push(
|
suites.push(
|
||||||
Benchmark.Suite('flatten deep')
|
Benchmark.Suite('flatten deep')
|
||||||
.add('Lo-Dash', function() {
|
.add('Lo-Dash', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user