more part one

JavaScript performance comparison

Test case created by Garrett Johsnon

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    // faster [].slice
    function slice(args){
      var ret = [];
      for (var i = 0; i < arguments.length; i++) {
        args[i] = arguments[i];
      }
      return ret;
    }
   
    // what we have currently
    function sendA(msg){
      var args = [];
      if (Array.isArray(msg)) {
        args = msg;
      } else {
        for (var i = 0; i < arguments.length; i++) {
           args[i] = arguments[i];
        }
      }
      return args;
    }
   
    // similar to "A", but refactored a bit
    function sendB(msg){
      var args = [];
      if (Array.isArray(msg)) args = msg
      else args = slice(arguments)
      return args;
    }
   
    // don't check for array
    function sendC(msg){
      if (arguments.length > 1) msg = slice(arguments);
      return msg;
    }
   
    // similar to "C", but kinda rest parameter hack
    function sendD(msg, more){
      if (null != more) msg = slice(arguments);
      return msg;
    }
};
</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
sendA
sendA('one', 'two', 'three', 'four', 'five');
pending…
sendB
sendB('one', 'two', 'three', 'four', 'five');
pending…
sendC
sendC('one', 'two', 'three', 'four', 'five');
pending…
sendD
sendD('one', 'two', 'three', 'four', 'five');
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

0 comments

Add a comment