api implementation #1

JavaScript performance comparison

Test case created by Kyle Simpson

Info

Testing effects of API implementation (use of more function calls) on performance

Preparation code

<script>
  var lib_1 = (function() {
 
   function check_list(list) {
    if (!list || list.length < 1) return false;
    return true;
   }
 
   function sum(list, type) {
    if (!check_list(list)) {
     if (type == "number") return 0;
     else return "";
    }
 
    var ret = list[0];
    for (var i = 1; i < list.length; i++) {
     if (typeof list[i] == type) {
      ret += list[i];
     }
    }
    return ret;
   }
 
   function sum_numbers(list) {
    return sum(list, "number");
   }
 
   function concat_strings(list) {
    return sum(list, "string");
   }
 
   return {
    SumNumbers: sum_numbers,
    ConcatStrings: concat_strings,
    Add: function(list) {
     if (!check_list(list)) {
      return;
     }
     else if (typeof list[0] == "number") {
      return sum_numbers(list);
     }
     else if (typeof list[0] == "string") {
      return concat_strings(list);
     }
    }
   }
  })();
 
  var lib_2 = (function() {
 
   function sum(list, type) {
    var ret = list[0];
    for (var i = 1; i < list.length; i++) {
     if (typeof list[i] == type) {
      ret += list[i];
     }
    }
    return ret;
   }
 
   return {
    SumNumbers: function(list) {
     if (!list || list.length < 1) return 0;
     return sum(list, "number");
    },
    ConcatStrings: function(list) {
     if (!list || list.length < 1) return "";
     return sum(list, "string");
    },
    Add: function(list) {
     if (!list || list.length < 1) return;
     return sum(list, (typeof list[0]));
    }
   }
  })();
 
  var list_of_nums = [5, 10, 3, 0, 12, 194.8 /* , ... */ ];
  var list_of_strings = ["a", "foo", "bar", "baz" /* , ... */ ];
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
lib_1 api design
lib_1.Add();
lib_1.Add(list_of_nums);
lib_1.Add(list_of_strings);
pending…
lib_2 api design
lib_2.Add();
lib_2.Add(list_of_nums);
lib_2.Add(list_of_strings);
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

1 comment

Morgan Roderick commented :

A very interesting test.

Kyle and I do not agree on whether or not there can be any conclusive recommendations based on the numbers in these tests... but I think we can all agree, that the numbers are quite interesting and show off weaknesses in the different JavaScript engines.

Add a comment