Netmask Validation (v8)

Revision 8 of this benchmark created by Chris Hoffman on


Setup

var masks = [
      0b11111111,
      0b11111110,
      0b11111100,
      0b11111000,
      0b11110000,
      0b11100000,
      0b11000000,
      0b10000000,
      0b00000000
    ];
    
    var data = [
      '255.255.255.255',
      '255.255.255.128',
      '255.255.255.192',
      '255.255.255.0',
      '255.255.255.224',
      '255.255.0.0',
      '255.0.0.0',
      '0.0.0.0',
    ]
    
    function test(x) { return x.includes(parseInt(x, 10)) }
    function validNetmask(netmask) {
      var parts = netmask.split('.');
      return parts.length === 4 && parts.every(test)
    }
    
    function validNetmaskRegex(netmask) {
    var nmFormat = /^(((0|128|192|224|240|248|252|254).0.0.0)|(255.(0|128|192|224|240|248|252|254).0.0)|(255.255.(0|128|192|224|240|248|252|254).0)|(255.255.255.(0|128|192|224|240|248|252|254)))$/;
    
      return !!netmask.match(nmFormat);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Regex
for (var i = 0, len = data.length; i < len; ++i) {
  validNetmaskRegex(data[i]);
}
ready
Mask
for (var i = 0, len = data.length; i < len; ++i) {
  validNetmask(data[i]);
}
ready

Revisions

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

  • Revision 1: published by Chris Hoffman on
  • Revision 2: published by Chris Hoffman on
  • Revision 3: published by Chris Hoffman on
  • Revision 4: published by Chris Hoffman on
  • Revision 5: published by Chris Hoffman on
  • Revision 7: published by Chris Hoffman on
  • Revision 8: published by Chris Hoffman on