String to integer magic (v2)

Revision 2 of this benchmark created by Wonderfool on


Description

Simple test cases to show methods for transforming strings pulled from the DOM into integers.

Preparation HTML

<div id="box1" style="width:200px; height:10px;">&nbsp;</div>
<div id="box2" style="width:400px; height:5px;">&nbsp;</div>
<input type="hidden" value="20" id="testinput"/>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Setup

var box1 = $("#box1"),
    box2 = $("#box2"),
    box1el = box1[0],
    box2el = box2[0],
    testinput = $("#testinput"),
    testinputel = testinput[0];

Test runner

Ready to run.

Testing in
TestOps/sec
jquery get and parseint
var a = parseInt($("#box1").css('width'));
var b = parseInt($("#box2").css('width'));
var c = parseInt($("#testinput").val());
ready
jquery get and parseint with set radix
var a = parseInt($("#box1").css('width'),10);
var b = parseInt($("#box2").css('width'),10);
var c = parseInt($("#testinput").val(),10);
ready
jquery get and slice bitshift
var a = ($("#box1").css('width').slice(0,-2)<<0);
var b = ($("#box2").css('width').slice(0,-2)<<0);
var c = ($("#testinput").val()<<0);
ready
parseInt cached
var a = parseInt(box1.css('width'));
var b = parseInt(box2.css('width'));
var c = parseInt(testinput.val());
ready
parseInt cached with set radix
var a = parseInt(box1.css('width'), 10);
var b = parseInt(box2.css('width'),10);
var c = parseInt(testinput.val(),10);
ready
slice bitshift cache
var a = box1.css('width').slice(0,-2)<<0;
var b = box2.css('width').slice(0,-2)<<0;
var c = testinput.val()<<0;
ready
el + slice + cast
var a = +box1el.style.width.slice(0, -2);
var b = +box2el.style.width.slice(0, -2);
var c = +testinputel.value.slice(0, -2);
ready
el + slice + notnot
var a = ~~box1el.style.width.slice(0, -2);
var b = ~~box2el.style.width.slice(0, -2);
var c = ~~testinputel.value.slice(0, -2);
ready
el + slice + bitshift
var a = box1el.style.width.slice(0, -2) >> 0;
var b = box2el.style.width.slice(0, -2) >> 0;
var c = testinputel.value.slice(0, -2) >> 0;
ready

Revisions

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

  • Revision 2: published by Wonderfool on