Run details:
Mozilla/5.0 (iPhone; CPU iPhone OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Mobile/15E148 Safari/604.1
Mobile Safari 16
iOS 16.3.1
Mobile
2 years ago
Test name Executions per second
slice 8860650.0 Ops/sec
regex 7949157.0 Ops/sec
brackets 5213679.0 Ops/sec
Script Preparation code:
x
 
var strIn = '1234-12-34 05:27:56Z';
var strOut = '';
var regex = /\d{4}-(\d{2})-(\d{2}) (\d{2})/;
Tests:
  • slice

     
    var month = strIn.slice(5, 7);
    var day = strIn.slice(8, 10);
    var hour = strIn.slice(11, 13);
  • regex

     
    var [_, month, day, hour] = strIn.match(regex)
  • brackets

     
    var month = strIn[5] + strIn[6];
    var day = strIn[8] + strIn[9];
    var hour = strIn[11] + strIn[12];