in JS what is more efficient for determining if a string is one of many options: compare with multiple ORs or switch?

Example: I want to see if a key press is one of 4 different keys.
Option 1:

if(e.key === "ArrowUp"   ||
   e.key === "ArrowDown" ||
   e.key === "Control"   ||
   e.key === "Escape"    || ){
   // do stuff
}

vs


 switch(e.key){
        case "ArrowUp":
        case "ArrowDown":
        case "Control":
        case "Escape":
            // do stuff
            break;
        default:
            break;
    }

There are many resources out there showing that a switch statement is more performant than multiple ifs. But what about a single if with multiple ORs vs a switch statement?

7

It was suggested to run some benchmarks and also include includes so here they are:


function testOr(k){
if(k === "ArrowUp"   ||
   k === "ArrowDown" ||
   k === "Control"   ||
   k === "Escape"    ){
   return true
 }
}

function testSwitch(k){
 switch(k){
        case "ArrowUp":
        case "ArrowDown":
        case "Control":
        case "Escape":
            return true
            break;
        default:
            break;
    }
}

function testIncludes(k){
  if([ "ArrowUp", "ArrowDown", "Control", "Escape"].includes(k)){return true}
}


function test(x, fn, k){
    const start = Date.now();
    for (let i=0; i<k; i++){
    fn(x)
  }
  console.log(`total time spent: ${Date.now()-start}`)
}


const iterations = [ 1000, 10000, 100000, 1000000 ]

// testing begins
iterations.forEach(t=>test("ArrowUp", testOr, t)) // finding first element
iterations.forEach(t=>test("Escape", testOr, t)) // finding last element
iterations.forEach(t=>test("asdf", testOr, t)) // finding nothing
// repeated for testSwitch and testIncludes

Results (in ms):

switch or includes input type iterations
0 0 0 first element 1000
2 2 4 first element 10000
20 17 40 first element 100000
206 195 378 first element 1000000
1 1 1 last element 1000
2 3 3 last element 10000
22 24 43 last element 100000
236 256 381 last element 1000000
1 0 1 not included 1000
2 2 3 not included 10000
28 27 36 not included 100000
273 270 392 not included 1000000

Plotted (lower = better):

It seems like the answer is:

  1. includes is less performant with scale
  2. when it comes to finding the first element, or performs the best (wins by 11ms)
  3. but when it comes to finding the last element, switch performs the best (wins by 20ms)
  4. when it comes to finding no elements at all, or is juuust better than switch by 3ms

Moral of the story: don’t use includes, switch is the way to go for the worst case scenario, or is the way to go for best case scenario.

6

@Null Salad, as you mentioned, the switch-case statement is generally more performant than an if-else statement, especially when dealing with large if-else ladders compared to their equivalent switch representation. You can find more details here

In this particular case, if you have a small number of conditions, the performance difference between if-else and switch-case is negligible. The only factor that matters is readability, and personally, I find if-else to be the better option.

New contributor

Isaac Blanco is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật