jquery arithmetic plugin

Benchmark created on


Description

See here: http://www.reddit.com/r/programming/comments/buosj/addanumbertoanothernumberinjavascriptimg/?sort=top

Preparation HTML

<script src="http://code.jquery.com/jquery-1.6.min.js"></script>
<div id="result1">
<script>
  (function($) {
  
   $.fn.extend({
  
    // $(...).number( num )
    //     Save the number 'num' in the jQuery data
    //     for the selected element(s), and return 'this'
    //     for chaining.
    // $(...).number()
    //     Return the number stored in the jQuery data
    //     for the selected elements(s).
    number: function(num) {
     if (arguments.length == 0) return this.data('number');
     this.data('number', num);
     return this;
    },
  
    // $(...).add( num )
    //     Add the number 'num' to the number stored
    //     in the jQuery data for the selected element(s)
    add: function(num) {
     return this.number(this.number() + num);
    },
  
    // $(...).subtract( num )
    //     Subtract the number 'num' from the number stored
    //     in the jQuery data for the selected element(s)
    subtract: function(num) {
     return this.number(this.number() - num);
    }
   });
  
  })(jQuery);
  
  var result2;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jquery
$('#result1').number(23).add(15).subtract(4).add(16).subtract(8).number()
ready
pure js
result2 = 23 + 15 - 4 + 16 - 8;
ready

Revisions

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