ipaddress to decimal and back

JavaScript performance comparison

Test case created by snbartell

Preparation code

<script>
  function dot2num1(dot) {
    var parts = dot.split(".");
    var res = 0;

    res += (parseInt(parts[0], 10) << 24) >>> 0;
    res += (parseInt(parts[1], 10) << 16) >>> 0;
    res += (parseInt(parts[2], 10) << 8) >>> 0;
    res += parseInt(parts[3], 10) >>> 0;

    return res;
  }

  function dot2num2(dot) {
    var d = dot.split('.');
    return ((((((+d[0]) * 256) + (+d[1])) * 256) + (+d[2])) * 256) + (+d[3]);
  }

  function num2dot1(num) {
    var part1 = num & 255;
    var part2 = ((num >> 8) & 255);
    var part3 = ((num >> 16) & 255);
    var part4 = ((num >> 24) & 255);

    return part4 + "." + part3 + "." + part2 + "." + part1;
  }

  function num2dot2(num) {
    var d = num % 256;
    for (var i = 3; i > 0; i--) {
      num = Math.floor(num / 256);
      d = num % 256 + '.' + d;
    }
    return d;
  }
</script>
    

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
dot2num1
dot2num1('255.255.255.255')
pending…
num2dot1
num2dot1(4294967295)
pending…
num2dot2
num2dot2(4294967295)
pending…
dot2num2
dot2num2('255.255.255.255')
pending…

Compare results of other browsers

Revisions

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

0 Comments