Skip to content

Commit

Permalink
Merge pull request #22 from IWANABETHATGUY/feature/optimization
Browse files Browse the repository at this point in the history
perf(cache length of array, object): improve performance
  • Loading branch information
planttheidea authored Nov 2, 2019
2 parents dcd8d8f + 6a51495 commit c30aae4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ function copy<T>(object: T, options?: FastCopy.Options): T {
}

let clone: any;

// arrays
if (isArray(object)) {
const objectLength = object.length;
cache.add(object);

// if strict, include non-standard properties
Expand All @@ -92,8 +92,7 @@ function copy<T>(object: T, options?: FastCopy.Options): T {
}

clone = new Constructor();

for (let index: number = 0; index < object.length; index++) {
for (let index: number = 0; index < objectLength; index++) {
clone[index] = handleCopy(object[index], cache);
}

Expand Down
12 changes: 6 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ export const getObjectCloneLoose: FastCopy.ObjectCloner = (

if (SUPPORTS.SYMBOL_PROPERTIES) {
const symbols: symbol[] = getOwnPropertySymbols(object);

if (symbols.length) {
for (let index = 0, symbol; index < symbols.length; index++) {
const symbolsLength = symbols.length;
if (symbolsLength) {
for (let index = 0, symbol; index < symbolsLength; index++) {
symbol = symbols[index];

if (propertyIsEnumerable.call(object, symbol)) {
Expand Down Expand Up @@ -141,11 +141,11 @@ export const getObjectCloneStrict: FastCopy.ObjectCloner = (
const properties: (string | symbol)[] = SUPPORTS.SYMBOL_PROPERTIES
? [].concat(getOwnPropertyNames(object), getOwnPropertySymbols(object))
: getOwnPropertyNames(object);

if (properties.length) {
const propertiesLength = properties.length;
if (propertiesLength) {
for (
let index = 0, property, descriptor;
index < properties.length;
index < propertiesLength;
index++
) {
property = properties[index];
Expand Down

0 comments on commit c30aae4

Please sign in to comment.