Skip to content

Commit

Permalink
Use nativeSlice when possible and adjust largeArraySize to accoun…
Browse files Browse the repository at this point in the history
…t for the recent `cachedContains` tweaks.
  • Loading branch information
jdalton committed Apr 6, 2013
1 parent 81ebaa8 commit 86fbeab
Show file tree
Hide file tree
Showing 16 changed files with 523 additions and 566 deletions.
24 changes: 3 additions & 21 deletions build.js
Expand Up @@ -175,7 +175,7 @@
'value': ['forOwn', 'isArray'], 'value': ['forOwn', 'isArray'],
'values': ['keys'], 'values': ['keys'],
'where': ['filter'], 'where': ['filter'],
'without': ['indexOf'], 'without': ['difference'],
'wrap': [], 'wrap': [],
'zip': ['max', 'pluck'], 'zip': ['max', 'pluck'],
'zipObject': [], 'zipObject': [],
Expand Down Expand Up @@ -2392,23 +2392,6 @@
'}' '}'
].join('\n')); ].join('\n'));


// replace `_.without`
source = replaceFunction(source, 'without', [
'function without(array) {',
' var index = -1,',
' length = array.length,',
' result = [];',
'',
' while (++index < length) {',
' var value = array[index];',
' if (indexOf(arguments, value, 1) < 0) {',
' result.push(value);',
' }',
' }',
' return result',
'}'
].join('\n'));

// add `_.findWhere` // add `_.findWhere`
source = source.replace(matchFunction(source, 'find'), function(match) { source = source.replace(matchFunction(source, 'find'), function(match) {
var indent = getIndent(match); var indent = getIndent(match);
Expand Down Expand Up @@ -2446,10 +2429,9 @@
}); });
}); });


// replace `slice` with `slice.call` // replace `slice` with `nativeSlice.call`
source = removeFunction(source, 'slice'); source = removeFunction(source, 'slice');
source = source.replace(/^(( *)setTimeout = context.setTimeout)([,;])/m, '$1,\n$2slice = arrayRef.slice$3'); source = source.replace(/([^.])\bslice\(/g, '$1nativeSlice.call(');
source = source.replace(/([^.]\bslice)\(/g, '$1.call(');


// replace `lodash.createCallback` references with `createCallback` // replace `lodash.createCallback` references with `createCallback`
if (!exposeCreateCallback) { if (!exposeCreateCallback) {
Expand Down
67 changes: 46 additions & 21 deletions dist/lodash.backbone.js
Expand Up @@ -33,6 +33,9 @@
/** Used to prefix keys to avoid issues with `__proto__` and properties on `Object.prototype` */ /** Used to prefix keys to avoid issues with `__proto__` and properties on `Object.prototype` */
var keyPrefix = +new Date + ''; var keyPrefix = +new Date + '';


/** Used as the size when optimizations are enabled for large arrays */
var largeArraySize = 200;

/** Used to match empty string literals in compiled template source */ /** Used to match empty string literals in compiled template source */
var reEmptyStringLeading = /\b__p \+= '';/g, var reEmptyStringLeading = /\b__p \+= '';/g,
reEmptyStringMiddle = /\b(__p \+=) '' \+/g, reEmptyStringMiddle = /\b(__p \+=) '' \+/g,
Expand Down Expand Up @@ -121,7 +124,6 @@
hasOwnProperty = objectRef.hasOwnProperty, hasOwnProperty = objectRef.hasOwnProperty,
push = arrayRef.push, push = arrayRef.push,
setTimeout = window.setTimeout, setTimeout = window.setTimeout,
slice = arrayRef.slice,
toString = objectRef.toString; toString = objectRef.toString;


/* Native method shortcuts for methods with the same name as other `lodash` methods */ /* Native method shortcuts for methods with the same name as other `lodash` methods */
Expand All @@ -132,7 +134,8 @@
nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys, nativeKeys = reNative.test(nativeKeys = Object.keys) && nativeKeys,
nativeMax = Math.max, nativeMax = Math.max,
nativeMin = Math.min, nativeMin = Math.min,
nativeRandom = Math.random; nativeRandom = Math.random,
nativeSlice = arrayRef.slice;


/** Detect various environments */ /** Detect various environments */
var isIeOpera = reNative.test(window.attachEvent), var isIeOpera = reNative.test(window.attachEvent),
Expand Down Expand Up @@ -574,7 +577,7 @@
*/ */
function clone(value) { function clone(value) {
return isObject(value) return isObject(value)
? (isArray(value) ? slice.call(value) : assign({}, value)) ? (isArray(value) ? nativeSlice.call(value) : assign({}, value))
: value; : value;
} }


Expand Down Expand Up @@ -1534,7 +1537,7 @@
* // => [['1', '2', '3'], ['4', '5', '6']] * // => [['1', '2', '3'], ['4', '5', '6']]
*/ */
function invoke(collection, methodName) { function invoke(collection, methodName) {
var args = slice.call(arguments, 2), var args = nativeSlice.call(arguments, 2),
index = -1, index = -1,
isFunc = typeof methodName == 'function', isFunc = typeof methodName == 'function',
length = collection ? collection.length : 0, length = collection ? collection.length : 0,
Expand Down Expand Up @@ -2064,7 +2067,7 @@
*/ */
function toArray(collection) { function toArray(collection) {
if (isArray(collection)) { if (isArray(collection)) {
return slice.call(collection); return nativeSlice.call(collection);
} }
if (collection && typeof collection.length == 'number') { if (collection && typeof collection.length == 'number') {
return map(collection); return map(collection);
Expand All @@ -2074,6 +2077,37 @@


/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/


/**
* Creates an array of `array` elements not present in the other arrays
* using strict equality for comparisons, i.e. `===`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to process.
* @param {Array} [array1, array2, ...] Arrays to check.
* @returns {Array} Returns a new array of `array` elements not present in the
* other arrays.
* @example
*
* _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
* // => [1, 3, 4]
*/
function difference(array) {
var index = -1,
length = array.length,
flattened = concat.apply(arrayRef, arguments),
result = [];

while (++index < length) {
var value = array[index];
if (indexOf(flattened, value, length) < 0) {
result.push(value);
}
}
return result;
}

/** /**
* Gets the first element of the `array`. If a number `n` is passed, the first * Gets the first element of the `array`. If a number `n` is passed, the first
* `n` elements of the `array` are returned. If a `callback` function is passed, * `n` elements of the `array` are returned. If a `callback` function is passed,
Expand Down Expand Up @@ -2148,7 +2182,7 @@
return array[0]; return array[0];
} }
} }
return slice.call(array, 0, nativeMin(nativeMax(0, n), length)); return nativeSlice.call(array, 0, nativeMin(nativeMax(0, n), length));
} }
} }


Expand Down Expand Up @@ -2266,7 +2300,7 @@
} else { } else {
n = (callback == null || thisArg) ? 1 : callback || n; n = (callback == null || thisArg) ? 1 : callback || n;
} }
return slice.call(array, 0, nativeMin(nativeMax(0, length - n), length)); return nativeSlice.call(array, 0, nativeMin(nativeMax(0, length - n), length));
} }


/** /**
Expand Down Expand Up @@ -2343,7 +2377,7 @@
return array[length - 1]; return array[length - 1];
} }
} }
return slice.call(array, nativeMax(0, length - n)); return nativeSlice.call(array, nativeMax(0, length - n));
} }
} }


Expand Down Expand Up @@ -2451,7 +2485,7 @@
} else { } else {
n = (callback == null || thisArg) ? 1 : nativeMax(0, callback); n = (callback == null || thisArg) ? 1 : nativeMax(0, callback);
} }
return slice.call(array, n); return nativeSlice.call(array, n);
} }


/** /**
Expand Down Expand Up @@ -2535,17 +2569,7 @@
* // => [2, 3, 4] * // => [2, 3, 4]
*/ */
function without(array) { function without(array) {
var index = -1, return difference(array, nativeSlice.call(arguments, 1));
length = array.length,
result = [];

while (++index < length) {
var value = array[index];
if (indexOf(arguments, value, 1) < 0) {
result.push(value);
}
}
return result
} }


/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
Expand Down Expand Up @@ -2577,7 +2601,7 @@
// (in V8 `Function#bind` is slower except when partially applied) // (in V8 `Function#bind` is slower except when partially applied)
return support.fastBind || (nativeBind && arguments.length > 2) return support.fastBind || (nativeBind && arguments.length > 2)
? nativeBind.call.apply(nativeBind, arguments) ? nativeBind.call.apply(nativeBind, arguments)
: createBound(func, thisArg, slice.call(arguments, 2)); : createBound(func, thisArg, nativeSlice.call(arguments, 2));
} }


/** /**
Expand Down Expand Up @@ -2961,6 +2985,7 @@
lodash.bindAll = bindAll; lodash.bindAll = bindAll;
lodash.countBy = countBy; lodash.countBy = countBy;
lodash.defaults = defaults; lodash.defaults = defaults;
lodash.difference = difference;
lodash.filter = filter; lodash.filter = filter;
lodash.forEach = forEach; lodash.forEach = forEach;
lodash.functions = functions; lodash.functions = functions;
Expand Down

0 comments on commit 86fbeab

Please sign in to comment.