Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.77
Chrome 103
Windows
Desktop
2 years ago
Test name Executions per second
dict 42533.8 Ops/sec
if 41561.2 Ops/sec
switch 41135.1 Ops/sec
Tests:
  • dict

    x
     
    const dict = {};
    dict['test1'] = 'sth1';
    dict['test2'] = 'sth2';
    dict['test3'] = 'sth3';
    let t = 'nn';
    console.log(dict[t]);
    t='test1';
    console.log(dict[t]);
    t='test2';
    console.log(dict[t]);
    t='test3';
    console.log(dict[t]);
  • if

     
    let t = 'nn';
    if(t === 'test1') {
      console.log('sth1');
    } else if(t == 'test2') {
     console.log('sth2'); 
    } else if(t == 'test3') {
     console.log('sth3'); 
    } else {
     console.log(undefined); 
    }
    t='test1';
    if(t === 'test1') {
      console.log('sth1');
    } else if(t == 'test2') {
     console.log('sth2'); 
    } else if(t == 'test3') {
     console.log('sth3'); 
    } else {
     console.log(undefined); 
    }
    t='test2';
    if(t === 'test1') {
      console.log('sth1');
    } else if(t == 'test2') {
     console.log('sth2'); 
    } else if(t == 'test3') {
     console.log('sth3'); 
    } else {
     console.log(undefined); 
    }
    t='test3';
    if(t === 'test1') {
      console.log('sth1');
    } else if(t == 'test2') {
     console.log('sth2'); 
    } else if(t == 'test3') {
     console.log('sth3'); 
    } else {
     console.log(undefined); 
    }
  • switch

     
    let t='nn';
    switch(t) {
      case 'test1':
        console.log('sth1');
        break;
      case 'test2':
        console.log('sth2');
        break;
      case 'test3':
        console.log('sth3');
        break;
      default:
        console.log(undefined);
    }
    t='test1';
    switch(t) {
      case 'test1':
        console.log('sth1');
        break;
      case 'test2':
        console.log('sth2');
        break;
      case 'test3':
        console.log('sth3');
        break;
      default:
        console.log(undefined);
    }
    t='test2';
    switch(t) {
      case 'test1':
        console.log('sth1');
        break;
      case 'test2':
        console.log('sth2');
        break;
      case 'test3':
        console.log('sth3');
        break;
      default:
        console.log(undefined);
    }
    t='test3';
    switch(t) {
      case 'test1':
        console.log('sth1');
        break;
      case 'test2':
        console.log('sth2');
        break;
      case 'test3':
        console.log('sth3');
        break;
      default:
        console.log(undefined);
    }