Skip to content

Loading…

Remove charAt calls #1173

Closed
wants to merge 1 commit into from

2 participants

@ameyms

Replace charAt calls with index lookup.
Index lookups are ~10% faster and save a few bytes
http://jsperf.com/charat-vs-index/2

@ameyms

I just looked at canary builds and looks like charAt is 8% faster than index. That leaves me confused.

@dnschnur
Flot member

Unfortunately string indexing isn't supported on IE7, so we can't use it yet.

The performance numbers are interesting, but actually not too surprising. I would guess that older browsers implement strings as simple arrays, making index notation the most direct access. Modern browsers use more complex structures, like ropes, to save memory and allocation time, and it's probably easier to optimize charAt, which is guaranteed to be operating on a string, than indexing, which depending on the underlying type.

I do like the [x] syntax, especially since it saves space, so this is worth revisiting once we bump the minimum requirements to IE9. That day can't come too soon, in my opinion.

@dnschnur dnschnur closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 25, 2013
  1. @ameyms

    Remove charAt calls

    ameyms committed
Showing with 6 additions and 6 deletions.
  1. +1 −1 jquery.flot.categories.js
  2. +4 −4 jquery.flot.js
  3. +1 −1 jquery.flot.time.js
View
2 jquery.flot.categories.js
@@ -162,7 +162,7 @@ as "categories" on the axis object, e.g. plot.getAxes().xaxis.categories.
var points = datapoints.points,
ps = datapoints.pointsize,
format = datapoints.format,
- formatColumn = axis.charAt(0),
+ formatColumn = axis[0],
index = getNextIndex(categories);
for (var i = 0; i < points.length; i += ps) {
View
8 jquery.flot.js
@@ -2988,14 +2988,14 @@ Licensed under the MIT license.
if (m[0] == null) {
m = [m, m];
}
- if (p.charAt(0) === "n") {
+ if (p[0] === "n") {
pos += "top:" + (m[1] + plotOffset.top) + "px;";
- } else if (p.charAt(0) === "s") {
+ } else if (p[0] === "s") {
pos += "bottom:" + (m[1] + plotOffset.bottom) + "px;";
}
- if (p.charAt(1) === "e") {
+ if (p[1] === "e") {
pos += "right:" + (m[0] + plotOffset.right) + "px;";
- } else if (p.charAt(1) === "w") {
+ } else if (p[1] === "w") {
pos += "left:" + (m[0] + plotOffset.left) + "px;";
}
var legend = $("<div class='legend'>" + table.replace("style='", "style='position:absolute;" + pos +";") + "</div>").appendTo(placeholder);
View
2 jquery.flot.time.js
@@ -65,7 +65,7 @@ API.txt for details.
for (var i = 0; i < fmt.length; ++i) {
- var c = fmt.charAt(i);
+ var c = fmt[i];
if (escape) {
switch (c) {
Something went wrong with that request. Please try again.