Skip to content
This repository has been archived by the owner on Jan 11, 2022. It is now read-only.

Avoid creating new array #100

Merged
merged 1 commit into from
Aug 10, 2021
Merged

Conversation

spirinvladimir
Copy link
Contributor

Tiny performance improvement with checking first two chars with '0' instead of checking new string wiht '00'

According MDN:
Array.prototype.slice()
The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array.

Performance tests:

> f_01 = n => {var x=0;while(n--) {var s=String(Math.random()*10); if (s[0] === '0' && s[1] === '0') x++}return x};
[Function: f_01]
> f_slice = n => {var x=0;while(n--) {var s=String(Math.random()*10); if (s.slice(0,2) === '00') x++}return x};
[Function: f_slice]
> n=1e6;console.time(n);f_slice(n);console.timeEnd(n);
1000000: 283.498ms
undefined
> n=1e6;console.time(n);f_01(n);console.timeEnd(n);
1000000: 261.283ms
undefined
> n=1e8;console.time(n);f_slice(n);console.timeEnd(n);
100000000: 25.068s
undefined
> n=1e8;console.time(n);f_01(n);console.timeEnd(n);
100000000: 23.241s
undefined

According MDN:
Array.prototype.slice()
The slice() method returns a shallow copy of a portion of an array *into a new array object* selected from start to end (end not included) where start and end represent the index of items in that array.
@coveralls
Copy link

Coverage Status

Coverage remained the same at 90.598% when pulling 5ebbfea on spirinvladimir:patch-1 into 75f3981 on ethereumjs:master.

Copy link
Contributor

@ryanio ryanio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @spirinvladimir, thanks for the PR! seems reasonable to me

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants