Impact of string conversion on a string

JavaScript performance comparison

Test case created by François REMY


In this test case, I check whether which way of converting any input to a string for type safety reason has the least impact on the performance in case a string is passed as argument.

Preparation code

Benchmark.prototype.setup = function() {
    window.a = function a(s) {
        return String(s).length;
    window.b = function b(s) {
        return (""+s).length;
    window.c = function a(s) {
        return String(s).length;
    window.d = function b(s) {
        return (""+s).length;

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
String function
window.tmp = a("Abc")==3
Empty string concatenation
window.tmp = b("Abc")==3
String function*
window.tmp = c("Abc")==3
Empty string concatenation*
window.tmp = d("Abc")==3

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Comment form temporarily disabled.

Add a comment