Remove charAt calls #1173

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants
@ameyms

ameyms commented Oct 25, 2013

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

@ameyms

This comment has been minimized.

Show comment
Hide comment
@ameyms

ameyms Oct 25, 2013

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

ameyms commented Oct 25, 2013

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

@dnschnur

This comment has been minimized.

Show comment
Hide comment
@dnschnur

dnschnur Oct 26, 2013

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.

Member

dnschnur commented Oct 26, 2013

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 Oct 26, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment