FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

repl: use String#repeat instead of Array#join by evanlucas · Pull Request #3900 · nodejs/node · GitHub

/ node Public

repl: use String#repeat instead of Array#join - #3900

Merged
evanlucas merged 1 commit into
nodejs:masterfrom
evanlucas:stringrepeat
Dec 21, 2015
Merged

repl: use String#repeat instead of Array#join#3900
evanlucas merged 1 commit into
nodejs:masterfrom
evanlucas:stringrepeat

Conversation

Copy link
Copy Markdown
Contributor

String#repeat is quite a bit faster than new Array().join().

evanlucas added the repl Issues and PRs related to the REPL subsystem. label Nov 18, 2015

Copy link
Copy Markdown
Contributor

LGTM, evidence of said perf would be nice of course.

Copy link
Copy Markdown
Contributor Author

In the common case here:

This is testing:

'  '.repeat(1)

// vs

new Array(2).join('  ')

String#repeat 14.5 ns/op
Array#join 255.3 ns/op

cjihrig commented Nov 18, 2015

Copy link
Copy Markdown
Contributor

LGTM. The speedup is nice. Not sure how important it is in the REPL.

Copy link
Copy Markdown
Contributor Author

The advantage is the speedup in showing the prompt IMO

cjihrig commented Nov 18, 2015

Copy link
Copy Markdown
Contributor

I don't think anyone will perceive < 1us :-)

It's still nice to know for more performance critical places.

Copy link
Copy Markdown
Contributor Author

yea, true

Copy link
Copy Markdown
Contributor

It seems that the only place where this pattern emerged is in repl.js, unless my grep is not revealing enough.

$ grep -r 'new Array(' .
./dns.js:      var args = new Array(arguments.length + 1);
./domain.js:    var args = new Array(len - 1);
./events.js:      args = new Array(len - 1);
./events.js:  var copy = new Array(i);
./fs.js:  const chunks = new Array(len);
./internal/child_process.js:  // Don't concat() a new Array() because it would be sparse, and
./net.js:    var chunks = new Array(data.length << 1);
./querystring.js:var hexTable = new Array(256);
./repl.js:    var levelInd = new Array(this.lines.level.length).join('..');
./repl.js:    self.lines.push(new Array(self.lines.level.length).join('  ') + cmd);
./timers.js:      var args = new Array(length - 2);
./timers.js:      var args = new Array(length - 2);
./timers.js:      args = new Array(len - 1);
./util.js:  var output = new Array(value.length);

Comment thread lib/repl.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This could fit on a single line.

Copy link
Copy Markdown
Contributor

LGTM with style nit. By the way: https://jsperf.com/string-repeat-native-vs-array-join-vs-loop

Copy link
Copy Markdown
Contributor Author

Comment thread lib/repl.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Please this one too :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

ahh missed that one. Fixed

Copy link
Copy Markdown
Contributor

CI is happy, let's land it!

Copy link
Copy Markdown
Contributor Author

k, landing now

String#repeat is quite a bit faster than new Array().join().

PR-URL: nodejs#3900
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
evanlucas closed this Dec 21, 2015
evanlucas deleted the stringrepeat branch December 21, 2015 17:00
evanlucas merged commit 50125e2 into nodejs:master Dec 21, 2015

Copy link
Copy Markdown
Contributor Author

Landed in 50125e2. Thanks!

Fishrock123 pushed a commit to Fishrock123/node that referenced this pull request Dec 22, 2015
String#repeat is quite a bit faster than new Array().join().

PR-URL: nodejs#3900
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
Fishrock123 pushed a commit to Fishrock123/node that referenced this pull request Jan 6, 2016
String#repeat is quite a bit faster than new Array().join().

PR-URL: nodejs#3900
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
MylesBorins pushed a commit that referenced this pull request Jan 13, 2016
String#repeat is quite a bit faster than new Array().join().

PR-URL: #3900
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
MylesBorins pushed a commit that referenced this pull request Jan 19, 2016
String#repeat is quite a bit faster than new Array().join().

PR-URL: #3900
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
MylesBorins mentioned this pull request Jan 19, 2016
scovetta pushed a commit to scovetta/node that referenced this pull request Apr 2, 2016
String#repeat is quite a bit faster than new Array().join().

PR-URL: nodejs#3900
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

repl Issues and PRs related to the REPL subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants


Back | FazBrowse Home | New Git URL