Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

benchmark: replace [].join() with ''.repeat() #12170

Closed
wants to merge 1 commit into from
Closed

benchmark: replace [].join() with ''.repeat() #12170

wants to merge 1 commit into from

Conversation

vsemozhetbyt
Copy link
Contributor

@vsemozhetbyt vsemozhetbyt commented Apr 2, 2017

Checklist
Affected core subsystem(s)

benchmark

This change is mostly for readability as the edited fragments are not performance-wise concerned. However, a benchmark to compare both ways of long string creation is added to be on the safe side.

Results of the added benchmark with the last node.8.0.0-nightly20170402

es\string-repeat.js size=10 encoding="ascii" mode="Array" n=1000: 290,692.60419876396
es\string-repeat.js size=1000 encoding="ascii" mode="Array" n=1000: 102,836.05382685862
es\string-repeat.js size=1000000 encoding="ascii" mode="Array" n=1000: 135.32093280569134

es\string-repeat.js size=10 encoding="utf8" mode="Array" n=1000: 288,349.3871854649
es\string-repeat.js size=1000 encoding="utf8" mode="Array" n=1000: 94,707.65121331392
es\string-repeat.js size=1000000 encoding="utf8" mode="Array" n=1000: 115.63063143181027

es\string-repeat.js size=10 encoding="ascii" mode="repeat" n=1000: 1,551,397.8094262932
es\string-repeat.js size=1000 encoding="ascii" mode="repeat" n=1000: 695,483.8063550529
es\string-repeat.js size=1000000 encoding="ascii" mode="repeat" n=1000: 467,033.2877975878

es\string-repeat.js size=10 encoding="utf8" mode="repeat" n=1000: 1,659,899.8084475622
es\string-repeat.js size=1000 encoding="utf8" mode="repeat" n=1000: 661,734.5385725063
es\string-repeat.js size=1000000 encoding="utf8" mode="repeat" n=1000: 455,820.924368369

@nodejs-github-bot nodejs-github-bot added the benchmark Issues and PRs related to the benchmark subsystem. label Apr 2, 2017
@vsemozhetbyt
Copy link
Contributor Author

bench.end(n);
}

assert.strictEqual([...str].length, size);
Copy link
Member

Choose a reason for hiding this comment

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

Why [...str].length instead of just str.length?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is unicode-proof, it counts code points, not char codes:

> '\ud83d\udc0e'.repeat(10).length
20
> [...'\ud83d\udc0e'.repeat(10)].length
10


if (conf.mode === 'Array') {
bench.start();
for (let i = 0; i < n; i++) str = new Array(size + 1).join(character);
Copy link
Member

Choose a reason for hiding this comment

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

I like this style but I think it's not widely used in the codebase.
Is it better to put the for body in the next line?

Also is it ok to use let in for loops like this now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will wrap to conform.

It seems let in loops is mostly optimized now. At least they are rather common in tests or benchmarks.

Also add a benchmark to compare both ways to create strings.
@vsemozhetbyt
Copy link
Contributor Author

jasnell pushed a commit that referenced this pull request Apr 4, 2017
Also add a benchmark to compare both ways to create strings.

PR-URL: #12170
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@jasnell
Copy link
Member

jasnell commented Apr 4, 2017

Landed in 74dc3bf

@jasnell jasnell closed this Apr 4, 2017
@vsemozhetbyt vsemozhetbyt deleted the string-repeat branch April 4, 2017 16:39
@jasnell jasnell mentioned this pull request Apr 4, 2017
italoacasas pushed a commit to italoacasas/node that referenced this pull request Apr 10, 2017
Also add a benchmark to compare both ways to create strings.

PR-URL: nodejs#12170
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@italoacasas italoacasas mentioned this pull request Apr 10, 2017
2 tasks
JacksonTian added a commit to JacksonTian/node that referenced this pull request Apr 11, 2017
@MylesBorins
Copy link
Contributor

lts?

evanlucas pushed a commit that referenced this pull request Apr 25, 2017
Refs: #12170

PR-URL: #12317
Reviewed-By: James M Snell <jasnell@gmail.com>
evanlucas pushed a commit that referenced this pull request May 1, 2017
Refs: #12170

PR-URL: #12317
Reviewed-By: James M Snell <jasnell@gmail.com>
evanlucas pushed a commit that referenced this pull request May 2, 2017
Refs: #12170

PR-URL: #12317
Reviewed-By: James M Snell <jasnell@gmail.com>
@gibfahn
Copy link
Member

gibfahn commented May 15, 2017

ping @vsemozhetbyt , should this be backported to v6.x?

@vsemozhetbyt
Copy link
Contributor Author

vsemozhetbyt commented May 15, 2017

@gibfahn I am not sure. If we have some +1 I can try to backport though if this does not land cleanly.

@gibfahn gibfahn mentioned this pull request Jun 15, 2017
3 tasks
@gibfahn
Copy link
Member

gibfahn commented Jun 17, 2017

I suspect that it's not worth trying to backport any more complex performance improvements to v6.x, all the benchmarks will have to be rerun etc.

Having said that, if anyone is willing to do the work, then by all means do backport perf improvements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
benchmark Issues and PRs related to the benchmark subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants