Skip to content

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

Closed
vsemozhetbyt wants to merge 1 commit into
nodejs:masterfrom
vsemozhetbyt:string-repeat
Closed

benchmark: replace [].join() with ''.repeat()#12170
vsemozhetbyt wants to merge 1 commit into
nodejs:masterfrom
vsemozhetbyt:string-repeat

Conversation

@vsemozhetbyt

@vsemozhetbytvsemozhetbyt commented Apr 2, 2017

Copy link
Copy Markdown
Contributor
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-botnodejs-github-bot added the benchmark Issues and PRs related to the benchmark subsystem. label Apr 2, 2017
@vsemozhetbyt

Copy link
Copy Markdown
ContributorAuthor

Comment threadbenchmark/es/string-repeat.js Outdated

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

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

Comment threadbenchmark/es/string-repeat.js Outdated

Copy link
Copy Markdown
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
Copy Markdown
ContributorAuthor

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
Copy Markdown
ContributorAuthor

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
Copy Markdown
Member

Landed in 74dc3bf

@jasnelljasnell closed this Apr 4, 2017
@vsemozhetbyt
vsemozhetbyt deleted the string-repeat branch April 4, 2017 16:39
@jasnelljasnell 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>
@italoacasasitaloacasas 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
Copy Markdown
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
Copy Markdown
Member

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

@vsemozhetbyt

vsemozhetbyt commented May 15, 2017

Copy link
Copy Markdown
ContributorAuthor

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

@gibfahngibfahn mentioned this pull request Jun 15, 2017
3 tasks
@gibfahn

Copy link
Copy Markdown
Member

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 freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

benchmarkIssues and PRs related to the benchmark subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@vsemozhetbyt@jasnell@MylesBorins@gibfahn@lpinca@nodejs-github-bot