Script Preparation code:
x
 
var cases = [
  { nx: undefined, ny: 12 },
  { ny: 12, nx: undefined },
  { nx: undefined, ny: undefined },
  { nx: 12, ny: 12 }
]
var style = { transform: null }
Tests:
  • Typical Code

     
    for (const {nx, ny} of cases) {
      if (nx !== undefined || ny !== undefined) {
              style.transform = nx !== undefined
                  ? (ny !== undefined
                      ? `translate(${nx}px, ${ny}px)`
                      : `translateX(${nx}px)`)
                  : `translateY(${ny}px)`;
      }
    }
  • Bit-shift

     
    for (const {nx, ny} of cases) {
      const sx = nx !== undefined // state_x
      const sy = ny !== undefined // state_y
      
      const state = 
          (sx && ! sy ? 1 : 0)
        | (sy && ! sx ? 2 : 0)
        | (sx && sy ? 4 : 0)
        | ( ! sx || ! sy ? 8 : 0)
      
      switch (state) {
        case 8: // neither
          style.transform = ''
          break
        case 4: // both
          style.transform = `translate(${nx}px, ${ny}px)`
          break
        case 2: // only y
          style.transform = `translateY(${ny}px)`
          break
        case 1: // only x
          style.transform = `translateX(${nx}px)`
          break;
      }  
    }
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Typical Code
    Bit-shift

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 5 months ago)
Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1
Mobile Safari 18 on iOS 18.0
View result in a separate tab
Test name Executions per second
Typical Code 1295169.1 Ops/sec
Bit-shift 1732554.9 Ops/sec