Skip to content

Instantly share code, notes, and snippets.

@bga
Created April 30, 2010 05:09
Show Gist options
  • Save bga/384781 to your computer and use it in GitHub Desktop.
Save bga/384781 to your computer and use it in GitHub Desktop.
var i,n=1000000;
var a="123";
var b=Object(a);
console.time("Primitive.charAt");
i=n;
while(i--)
a.charAt(0);
console.timeEnd("Primitive.charAt");
console.time("Object.charAt");
i=n;
while(i--)
b.charAt(0);
console.timeEnd("Object.charAt");
console.time("Primitive.substr");
i=n;
while(i--)
a.substr(0,1);
console.timeEnd("Primitive.substr");
console.time("Object.substr");
i=n;
while(i--)
b.substr(0,1);
console.timeEnd("Object.substr");
console.time("Primitive.slice");
i=n;
while(i--)
a.slice(0,1);
console.timeEnd("Primitive.slice");
console.time("Object.slice");
i=n;
while(i--)
b.slice(0,1);
console.timeEnd("Object.slice");
console.time("Primitive[]");
i=n;
while(i--)
a[0];
console.timeEnd("Primitive[]");
console.time("Object[]");
i=n;
while(i--)
b[0];
console.timeEnd("Object[]");
/*
ff 3.6
Primitive.charAt: 922ms
Object.charAt: 928ms
Primitive.substr: 1067ms
Object.substr: 1088ms
Primitive.slice: 1056ms
Object.slice: 1123ms
Primitive[]: 478ms
Object[]: 538ms
chrome 4
Primitive.charAt: 1070
Object.charAt: 1087
Primitive.substr: 1000
Object.substr: 1122
Primitive.slice: 977
Object.slice: 1112
Primitive[]: 971
Object[]: 955
opera 10.50
Primitive.charAt: 670
Object.charAt: 618
Primitive.substr: 635
Object.substr: 634
Primitive.slice: 644
Object.slice: 633
Primitive[]: 537
Object[]: 503
ie7 n=100000 String[] not supported
Primitive.charAt: 671
Object.charAt: 942
Primitive.substr: 661
Object.substr: 961
Primitive.slice: 651
Object.slice: 952
ie8 n=200000 String[] not supported
Primitive.charAt: 731
Object.charAt: 741
Primitive.substr: 661
Object.substr: 781
Primitive.slice: 661
Object.slice: 832
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment