Optimize lodash function to avoid slow path for arrays.

Former-commit-id: 24e2c4ac2525ae38e214fdb0ea8e3ab664d784b7
This commit is contained in:
John-David Dalton
2013-03-09 18:24:29 -08:00
parent 31e4e631f9
commit ee4542a699
8 changed files with 164 additions and 159 deletions

19
dist/lodash.compat.js vendored
View File

@@ -213,14 +213,15 @@
* implicitly or explicitly included in the build.
*
* The chainable wrapper functions are:
* `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`, `compose`,
* `concat`, `countBy`, `debounce`, `defaults`, `defer`, `delay`, `difference`,
* `filter`, `flatten`, `forEach`, `forIn`, `forOwn`, `functions`, `groupBy`,
* `initial`, `intersection`, `invert`, `invoke`, `keys`, `map`, `max`, `memoize`,
* `merge`, `min`, `object`, `omit`, `once`, `pairs`, `partial`, `partialRight`,
* `pick`, `pluck`, `push`, `range`, `reject`, `rest`, `reverse`, `shuffle`,
* `slice`, `sort`, `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`,
* `union`, `uniq`, `unshift`, `values`, `where`, `without`, `wrap`, and `zip`
* `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`,
* `compose`, `concat`, `countBy`, `createCallback`, `debounce`, `defaults`,
* `defer`, `delay`, `difference`, `filter`, `flatten`, `forEach`, `forIn`,
* `forOwn`, `functions`, `groupBy`, `initial`, `intersection`, `invert`,
* `invoke`, `keys`, `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`,
* `once`, `pairs`, `partial`, `partialRight`, `pick`, `pluck`, `push`, `range`,
* `reject`, `rest`, `reverse`, `shuffle`, `slice`, `sort`, `sortBy`, `splice`,
* `tap`, `throttle`, `times`, `toArray`, `union`, `uniq`, `unshift`, `values`,
* `where`, `without`, `wrap`, and `zip`
*
* The non-chainable wrapper functions are:
* `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `has`,
@@ -242,7 +243,7 @@
*/
function lodash(value) {
// don't wrap if already wrapped, even if wrapped by a different `lodash` constructor
return (value && typeof value == 'object' && hasOwnProperty.call(value, '__wrapped__'))
return (value && typeof value == 'object' && !isArray(value) && hasOwnProperty.call(value, '__wrapped__'))
? value
: new lodashWrapper(value);
}

View File

@@ -4,7 +4,7 @@
* Build: `lodash -o ./dist/lodash.compat.js`
* Underscore.js 1.4.4 underscorejs.org/LICENSE
*/
;(function(n){function t(r){function a(n){return n&&typeof n=="object"&&Vt.call(n,"__wrapped__")?n:new L(n)}function B(n,t,e){t||(t=0);var r=n.length,u=r-t>=(e||f);if(u){var a={};for(e=t-1;++e<r;){var o=n[e]+"";(Vt.call(a,o)?a[o]:a[o]=[]).push(n[e])}}return function(e){if(u){var r=e+"";return Vt.call(a,r)&&-1<mt(a[r],e)}return-1<mt(n,e,t)}}function F(n){return n.charCodeAt(0)}function q(n,t){var e=n.b,r=t.b;if(n=n.a,t=t.a,n!==t){if(n>t||typeof n=="undefined")return 1;if(n<t||typeof t=="undefined")return-1
;(function(n){function t(r){function a(n){return n&&typeof n=="object"&&!ve(n)&&Vt.call(n,"__wrapped__")?n:new L(n)}function B(n,t,e){t||(t=0);var r=n.length,u=r-t>=(e||f);if(u){var a={};for(e=t-1;++e<r;){var o=n[e]+"";(Vt.call(a,o)?a[o]:a[o]=[]).push(n[e])}}return function(e){if(u){var r=e+"";return Vt.call(a,r)&&-1<mt(a[r],e)}return-1<mt(n,e,t)}}function F(n){return n.charCodeAt(0)}function q(n,t){var e=n.b,r=t.b;if(n=n.a,t=t.a,n!==t){if(n>t||typeof n=="undefined")return 1;if(n<t||typeof t=="undefined")return-1
}return e<r?-1:1}function R(n,t,e,r){function u(){var f=arguments,c=o?this:t;return a||(n=t[i]),e.length&&(f=f.length?(f=U(f),r?f.concat(e):e.concat(f)):e),this instanceof u?(M.prototype=n.prototype,c=new M,M.prototype=null,f=n.apply(c,f),nt(f)?f:c):n.apply(c,f)}var a=Z(n),o=!e,i=t;return o&&(e=t),a||(t=n),u}function T(){for(var n,t={f:_,b:"k(m)",c:"",e:"",g:"",h:!0},e=0;n=arguments[e];e++)for(var r in n)t[r]=n[r];if(n=t.a,t.d=/^[^,]+/.exec(n)[0],e=Pt,r="var i,m="+t.d+",u=m;if(!m)return u;"+t.g+";",t.b?(r+="var n=m.length;i=-1;if("+t.b+"){",ce.unindexedChars&&(r+="if(l(m)){m=m.split('')}"),r+="while(++i<n){"+t.e+"}}else{"):ce.nonEnumArgs&&(r+="var n=m.length;i=-1;if(n&&j(m)){while(++i<n){i+='';"+t.e+"}}else{"),ce.enumPrototypes&&(r+="var v=typeof m=='function';"),ce.fastKeys&&t.h?(r+="var s=-1,t=r[typeof m]?p(m):[],n=t.length;while(++s<n){i=t[s];",ce.enumPrototypes&&(r+="if(!(v&&i=='prototype')){"),r+=t.e,ce.enumPrototypes&&(r+="}")):(r+="for(i in m){",(ce.enumPrototypes||t.h)&&(r+="if(",ce.enumPrototypes&&(r+="!(v&&i=='prototype')"),ce.enumPrototypes&&t.h&&(r+="&&"),t.h&&(r+="h.call(m,i)"),r+="){"),r+=t.e+";",(ce.enumPrototypes||t.h)&&(r+="}")),r+="}",ce.nonEnumShadows){r+="var f=m.constructor;";
for(var u=0;7>u;u++)r+="i='"+t.f[u]+"';if(","constructor"==t.f[u]&&(r+="!(f&&f.prototype===m)&&"),r+="h.call(m,i)){"+t.e+"}"}return(t.b||ce.nonEnumArgs)&&(r+="}"),r+=t.c+";return u",e("h,j,k,l,o,r,p","return function("+n+"){"+r+"}")(Vt,G,ve,et,a,I,ne)}function D(n){return"\\"+N[n]}function z(n){return ye[n]}function K(n){return typeof n.toString!="function"&&typeof(n+"")=="string"}function L(n){this.__wrapped__=n}function M(){}function U(n,t,e){t||(t=0),typeof e=="undefined"&&(e=n?n.length:0);var r=-1;
e=e-t||0;for(var u=Et(0>e?0:e);++r<e;)u[r]=n[t+r];return u}function V(n){return he[n]}function G(n){return Qt.call(n)==w}function H(n){var t=!1;if(!n||typeof n!="object"||G(n))return t;var e=n.constructor;return!Z(e)&&(ce.nodeClass||!K(n))||e instanceof e?ce.ownLast?(pe(n,function(n,e,r){return t=Vt.call(r,e),!1}),!0===t):(pe(n,function(n,e){t=e}),!1===t||Vt.call(n,t)):t}function J(n){var t=[];return se(n,function(n,e){t.push(e)}),t}function Q(n,t,r,u,o,i){var f=n;if(typeof t=="function"&&(u=r,r=t,t=!1),typeof r=="function"){if(r=typeof u=="undefined"?r:a.createCallback(r,u,1),f=r(f),typeof f!="undefined")return f;

19
dist/lodash.js vendored
View File

@@ -207,14 +207,15 @@
* implicitly or explicitly included in the build.
*
* The chainable wrapper functions are:
* `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`, `compose`,
* `concat`, `countBy`, `debounce`, `defaults`, `defer`, `delay`, `difference`,
* `filter`, `flatten`, `forEach`, `forIn`, `forOwn`, `functions`, `groupBy`,
* `initial`, `intersection`, `invert`, `invoke`, `keys`, `map`, `max`, `memoize`,
* `merge`, `min`, `object`, `omit`, `once`, `pairs`, `partial`, `partialRight`,
* `pick`, `pluck`, `push`, `range`, `reject`, `rest`, `reverse`, `shuffle`,
* `slice`, `sort`, `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`,
* `union`, `uniq`, `unshift`, `values`, `where`, `without`, `wrap`, and `zip`
* `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`,
* `compose`, `concat`, `countBy`, `createCallback`, `debounce`, `defaults`,
* `defer`, `delay`, `difference`, `filter`, `flatten`, `forEach`, `forIn`,
* `forOwn`, `functions`, `groupBy`, `initial`, `intersection`, `invert`,
* `invoke`, `keys`, `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`,
* `once`, `pairs`, `partial`, `partialRight`, `pick`, `pluck`, `push`, `range`,
* `reject`, `rest`, `reverse`, `shuffle`, `slice`, `sort`, `sortBy`, `splice`,
* `tap`, `throttle`, `times`, `toArray`, `union`, `uniq`, `unshift`, `values`,
* `where`, `without`, `wrap`, and `zip`
*
* The non-chainable wrapper functions are:
* `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `has`,
@@ -236,7 +237,7 @@
*/
function lodash(value) {
// don't wrap if already wrapped, even if wrapped by a different `lodash` constructor
return (value && typeof value == 'object' && hasOwnProperty.call(value, '__wrapped__'))
return (value && typeof value == 'object' && !isArray(value) && hasOwnProperty.call(value, '__wrapped__'))
? value
: new lodashWrapper(value);
}

2
dist/lodash.min.js vendored
View File

@@ -4,7 +4,7 @@
* Build: `lodash modern -o ./dist/lodash.js`
* Underscore.js 1.4.4 underscorejs.org/LICENSE
*/
;(function(n){function t(r){function a(n){return n&&typeof n=="object"&&Mt.call(n,"__wrapped__")?n:new z(n)}function $(n,t,e){t||(t=0);var r=n.length,u=r-t>=(e||f);if(u){var a={};for(e=t-1;++e<r;){var o=n[e]+"";(Mt.call(a,o)?a[o]:a[o]=[]).push(n[e])}}return function(e){if(u){var r=e+"";return Mt.call(a,r)&&-1<gt(a[r],e)}return-1<gt(n,e,t)}}function B(n){return n.charCodeAt(0)}function F(n,t){var e=n.b,r=t.b;if(n=n.a,t=t.a,n!==t){if(n>t||typeof n=="undefined")return 1;if(n<t||typeof t=="undefined")return-1
;(function(n){function t(r){function a(n){return n&&typeof n=="object"&&!ie(n)&&Mt.call(n,"__wrapped__")?n:new z(n)}function $(n,t,e){t||(t=0);var r=n.length,u=r-t>=(e||f);if(u){var a={};for(e=t-1;++e<r;){var o=n[e]+"";(Mt.call(a,o)?a[o]:a[o]=[]).push(n[e])}}return function(e){if(u){var r=e+"";return Mt.call(a,r)&&-1<gt(a[r],e)}return-1<gt(n,e,t)}}function B(n){return n.charCodeAt(0)}function F(n,t){var e=n.b,r=t.b;if(n=n.a,t=t.a,n!==t){if(n>t||typeof n=="undefined")return 1;if(n<t||typeof t=="undefined")return-1
}return e<r?-1:1}function q(n,t,e,r){function u(){var f=arguments,c=o?this:t;return a||(n=t[i]),e.length&&(f=f.length?(f=P(f),r?f.concat(e):e.concat(f)):e),this instanceof u?(K.prototype=n.prototype,c=new K,K.prototype=null,f=n.apply(c,f),X(f)?f:c):n.apply(c,f)}var a=W(n),o=!e,i=t;return o&&(e=t),a||(t=n),u}function R(){for(var n,t={b:"k(m)",c:"",e:"",g:"",h:!0},e=0;n=arguments[e];e++)for(var r in n)t[r]=n[r];return n=t.a,t.d=/^[^,]+/.exec(n)[0],e=Nt,r="var i,m="+t.d+",u=m;if(!m)return u;"+t.g+";",t.b&&(r+="var n=m.length;i=-1;if("+t.b+"){while(++i<n){"+t.e+"}}else{"),re.fastKeys&&t.h?r+="var s=-1,t=r[typeof m]?p(m):[],n=t.length;while(++s<n){i=t[s];"+t.e+"}":(r+="for(i in m){",t.h&&(r+="if(",t.h&&(r+="h.call(m,i)"),r+="){"),r+=t.e+";",t.h&&(r+="}"),r+="}"),t.b&&(r+="}"),r+=t.c+";return u",e("h,j,k,l,o,r,p","return function("+n+"){"+r+"}")(Mt,U,ie,Z,a,A,Xt)
}function T(n){return"\\"+E[n]}function D(n){return ce[n]}function z(n){this.__wrapped__=n}function K(){}function P(n,t,e){t||(t=0),typeof e=="undefined"&&(e=n?n.length:0);var r=-1;e=e-t||0;for(var u=Ct(0>e?0:e);++r<e;)u[r]=n[t+r];return u}function M(n){return le[n]}function U(n){return Ht.call(n)==_}function V(n){var t=!1;if(!n||typeof n!="object"||U(n))return t;var e=n.constructor;return!W(e)||e instanceof e?(ae(n,function(n,e){t=e}),!1===t||Mt.call(n,t)):t}function G(n){var t=[];return oe(n,function(n,e){t.push(e)
}),t}function H(n,t,r,u,o,i){var f=n;if(typeof t=="function"&&(u=r,r=t,t=!1),typeof r=="function"){if(r=typeof u=="undefined"?r:a.createCallback(r,u,1),f=r(f),typeof f!="undefined")return f;f=n}if(u=X(f)){var c=Ht.call(f);if(!S[c])return f;var l=ie(f)}if(!u||!t)return u?l?P(f):pe({},f):f;switch(u=ee[c],c){case j:case w:return new u(+f);case C:case N:return new u(f);case O:return u(f.source,v.exec(f))}for(o||(o=[]),i||(i=[]),c=o.length;c--;)if(o[c]==n)return i[c];return f=l?u(f.length):{},l&&(Mt.call(n,"index")&&(f.index=n.index),Mt.call(n,"input")&&(f.input=n.input)),o.push(n),i.push(f),(l?ot:oe)(n,function(n,u){f[u]=H(n,t,r,e,o,i)

View File

@@ -148,14 +148,15 @@
* implicitly or explicitly included in the build.
*
* The chainable wrapper functions are:
* `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`, `compose`,
* `concat`, `countBy`, `debounce`, `defaults`, `defer`, `delay`, `difference`,
* `filter`, `flatten`, `forEach`, `forIn`, `forOwn`, `functions`, `groupBy`,
* `initial`, `intersection`, `invert`, `invoke`, `keys`, `map`, `max`, `memoize`,
* `merge`, `min`, `object`, `omit`, `once`, `pairs`, `partial`, `partialRight`,
* `pick`, `pluck`, `push`, `range`, `reject`, `rest`, `reverse`, `shuffle`,
* `slice`, `sort`, `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`,
* `union`, `uniq`, `unshift`, `values`, `where`, `without`, `wrap`, and `zip`
* `after`, `assign`, `bind`, `bindAll`, `bindKey`, `chain`, `compact`,
* `compose`, `concat`, `countBy`, `createCallback`, `debounce`, `defaults`,
* `defer`, `delay`, `difference`, `filter`, `flatten`, `forEach`, `forIn`,
* `forOwn`, `functions`, `groupBy`, `initial`, `intersection`, `invert`,
* `invoke`, `keys`, `map`, `max`, `memoize`, `merge`, `min`, `object`, `omit`,
* `once`, `pairs`, `partial`, `partialRight`, `pick`, `pluck`, `push`, `range`,
* `reject`, `rest`, `reverse`, `shuffle`, `slice`, `sort`, `sortBy`, `splice`,
* `tap`, `throttle`, `times`, `toArray`, `union`, `uniq`, `unshift`, `values`,
* `where`, `without`, `wrap`, and `zip`
*
* The non-chainable wrapper functions are:
* `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, `has`,