Netmask Validation (v2)

Revision 2 of this benchmark created by Chris Hoffman on


Setup

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)))$/;
    
    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 validNetmask(netmask) {
      var parts = ip.split('.').map(x => parseInt(x, 10));
      return parts.length === 4 && parts.every(x => x.includes(parseInt(x, 10)))
    }
    
    funciton validNetmaskRegex(netmask) {
      return nmFormat.test(netmask);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Regex
for (let i = 0, len = data.length; i < len; ++i) {
  validNetmaskRegex(data[i]);
}
ready
Mask
for (let 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