Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace some substring() and substr() calls with charAt()
Replace cases like foo.substring(0, 1) with foo.charAt(0). According to https://jsperf.com/substr-or-charat, charAt is about 20x faster on Chrome 54. This also saves a bit on code size. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=138557701
- Loading branch information
1 parent
13f48ca
commit 39184a6
Showing
7 changed files
with
180 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
39184a6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
string[1]
instead ofstring.charAt(1)
? Shorter and marginally more performant in Chrome, significantly more performant in Internet Explorer 11 -https://jsperf.com/substr-or-charat/2
(In Firefox, everything has more or less the same performance characteristics)
39184a6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe
string[1]
doesn't work in some older IEs. If we had a flag to indicate whether you need to support those, we could probably do that.39184a6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not work in the ancient Internet Explorer 7 and older. Are those really still supported?
http://netmarketshare.com/ desktop by version shows those as lower than a percent (combined).
http://gs.statcounter.com/#browser_version_partially_combined-ww-monthly-201610-201611-bar shows them (export the data to see) as lower than 0.12% (combined).
Anyway, you can use the ECMAScript 5 language output to control it, because it is part of that, according to https://kangax.github.io/compat-table/es5
39184a6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Google Search and others.
That's my suggestion as well
39184a6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or should we just have a separate pass that explicitly converts
charAt
calls (whether added by this pass, or in the original code) to bracket notation?39184a6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems very suited as a peephole optimization.