Skip to content

Instantly share code, notes, and snippets.

@corpix
Created November 4, 2012 19:06
Show Gist options
  • Select an option

  • Save corpix/4013125 to your computer and use it in GitHub Desktop.

Select an option

Save corpix/4013125 to your computer and use it in GitHub Desktop.
str[i] vs str.charAt(i)
var str = '',
max = 5000000,
tmp,
i = 0;
for( ; i < max; i++){
str += 'a';
}
console.log('Str length', str.length);
i = 0;
console.log('Getting %s values with str[i]', max);
console.time('str[]');
for( ; i < max; i++){
tmp = str[i];
}
console.timeEnd('str[]');
i = 0;
console.log('Getting %s values with str.charAt(i)', max);
console.time('str.charAt()');
for( ; i < max; i++){
tmp = str.charAt(i);
}
console.timeEnd('str.charAt()');
// On V8
// Str length 5000000
// Getting 5000000 values with str[i]
// str[]: 111ms
// Getting 5000000 values with str.charAt(i)
// str.charAt(): 39ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment