mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 11:27:50 +00:00
Optimize _.clone.
This commit is contained in:
64
lodash.js
64
lodash.js
@@ -1353,16 +1353,46 @@
|
|||||||
if (typeof result != 'undefined') {
|
if (typeof result != 'undefined') {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
var isObj = isObject(value);
|
var isArr = isArray(value),
|
||||||
if (isObj) {
|
isShallow = !isDeep;
|
||||||
|
|
||||||
|
if (isArr) {
|
||||||
|
result = isDeep ? value.constructor(value.length) : slice(value);
|
||||||
|
|
||||||
|
// add array properties assigned by `RegExp#exec`
|
||||||
|
if (typeof value[0] == 'string' && hasOwnProperty.call(value, 'index')) {
|
||||||
|
result.index = value.index;
|
||||||
|
result.input = value.input;
|
||||||
|
}
|
||||||
|
if (isShallow) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (!isObject(value)) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
var className = toString.call(value);
|
var className = toString.call(value);
|
||||||
if (!cloneableClasses[className] || (!support.nodeClass && isNode(value))) {
|
if (!cloneableClasses[className] || (!support.nodeClass && isNode(value))) {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
var isArgs = className == argsClass || (!support.argsClass && isArguments(value)),
|
||||||
|
isObj = !isArgs && className == objectClass;
|
||||||
|
|
||||||
|
if (isShallow && (isArgs || isObj)) {
|
||||||
|
result = baseAssign({}, value);
|
||||||
|
}
|
||||||
|
if (isShallow && isObj) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
var Ctor = value.constructor;
|
var Ctor = value.constructor;
|
||||||
if (className == objectClass && !(isFunction(Ctor) && (Ctor instanceof Ctor))) {
|
if (className == objectClass && !(isFunction(Ctor) && (Ctor instanceof Ctor))) {
|
||||||
Ctor = Object;
|
Ctor = Object;
|
||||||
}
|
}
|
||||||
|
if (isDeep && (isArgs || isObj)) {
|
||||||
|
result = new Ctor;
|
||||||
|
}
|
||||||
|
else {
|
||||||
switch (className) {
|
switch (className) {
|
||||||
case arrayBufferClass:
|
case arrayBufferClass:
|
||||||
return cloneBuffer(value);
|
return cloneBuffer(value);
|
||||||
@@ -1389,10 +1419,14 @@
|
|||||||
result.lastIndex = value.lastIndex;
|
result.lastIndex = value.lastIndex;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
var isArr = isArray(value);
|
}
|
||||||
|
if (isArgs) {
|
||||||
|
result.length = value.length;
|
||||||
|
}
|
||||||
|
if (isShallow) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
if (isDeep) {
|
if (isDeep) {
|
||||||
// check for circular references and return corresponding clone
|
// check for circular references and return corresponding clone
|
||||||
stackA || (stackA = []);
|
stackA || (stackA = []);
|
||||||
@@ -1404,26 +1438,6 @@
|
|||||||
return stackB[length];
|
return stackB[length];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result = isArr ? Ctor(value.length) : new Ctor;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
result = isArr ? slice(value) : baseAssign({}, value);
|
|
||||||
}
|
|
||||||
if (className == argsClass || (!support.argsClass && isArguments(value))) {
|
|
||||||
result.length = value.length;
|
|
||||||
}
|
|
||||||
// add array properties assigned by `RegExp#exec`
|
|
||||||
else if (isArr) {
|
|
||||||
if (hasOwnProperty.call(value, 'index')) {
|
|
||||||
result.index = value.index;
|
|
||||||
}
|
|
||||||
if (hasOwnProperty.call(value, 'input')) {
|
|
||||||
result.input = value.input;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// exit for shallow clone
|
|
||||||
if (!isDeep) {
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
// add the source value to the stack of traversed objects
|
// add the source value to the stack of traversed objects
|
||||||
// and associate it with its clone
|
// and associate it with its clone
|
||||||
|
|||||||
10
perf/perf.js
10
perf/perf.js
@@ -655,6 +655,16 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
suites.push(
|
||||||
|
Benchmark.Suite('`_.clone` with an array')
|
||||||
|
.add(buildName, '\
|
||||||
|
lodash.clone(numbers)'
|
||||||
|
)
|
||||||
|
.add(otherName, '\
|
||||||
|
_.clone(numbers)'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
suites.push(
|
suites.push(
|
||||||
Benchmark.Suite('`_.clone` with an object')
|
Benchmark.Suite('`_.clone` with an object')
|
||||||
.add(buildName, '\
|
.add(buildName, '\
|
||||||
|
|||||||
Reference in New Issue
Block a user