Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lookup table vs switch
(version: 0)
Comparing performance of:
lookup vs switch
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
lookup
const SOURCE = [ 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', ]; let a; var LOOKUP = { "ahello": function(v) { a = (v + "\n\n" + "hi"); }, "ahi": function(v) { a = (v + "\n\n" + "hello"); }, "abye": function(v) { a = (v + "\n\n" + "no"); }, "adie": function(v) { a = (v + "\n\n" + "you shot me"); }, "bhello": function(v) { a = (v + "\n\n" + "hi"); }, "bhi": function(v) { a = (v + "\n\n" + "hello"); }, "bbye": function(v) { a = (v + "\n\n" + "no"); }, "bdie": function(v) { a = (v + "\n\n" + "you shot me"); }, "hello": function(v) { a = (v + "\n\n" + "hi"); }, "hi": function(v) { a = (v + "\n\n" + "hello"); }, "bye": function(v) { a = (v + "\n\n" + "no"); }, "die": function(v) { a = (v + "\n\n" + "you shot me"); }, }; for(let value of SOURCE){ LOOKUP[value](value)}
switch
const SOURCE = [ 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', 'hello', 'hi', 'bye', 'die', 'die', 'bye', 'hi', 'hello', ]; let a; for(let value of SOURCE){ switch (value) { case "ahello": a = (value + "\n\n" + "hi"); case "ahi": a = (value + "\n\n" + "hello"); case "abye": a = (value + "\n\n" + "no"); case "adie": a = (value + "\n\n" + "you shot me"); case "bhello": a = (value + "\n\n" + "hi"); case "bhi": a = (value + "\n\n" + "hello"); case "bbye": a = (value + "\n\n" + "no"); case "bdie": a = (value + "\n\n" + "you shot me"); case "hello": a = (value + "\n\n" + "hi"); case "hi": a = (value + "\n\n" + "hello"); case "bye": a =(value + "\n\n" + "no"); case "die": a = (value + "\n\n" + "you shot me"); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lookup
switch
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.1 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 26 on iOS 18.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lookup
2362296.0 Ops/sec
switch
8618512.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The benchmark is defined as follows: ```json "Name": "lookup table vs switch", ``` This indicates that two different approaches will be compared: lookup tables and switches. **Overview of the Test Cases** There are two test cases: 1. **Lookup Table**: The script defines a constant `SOURCE` containing an array of strings, and another object `LOOKUP` with functions as values. Each function takes a string input `v` and assigns it to variable `a`, appending a specific string to `a`. The test loops through each value in the `SOURCE` array and calls the corresponding function from the `LOOKUP` object. 2. **Switch**: This script is similar to the first one, but instead of using an object with functions as values, it uses a switch statement to execute different code blocks based on the input string. **What's Going On?** The test aims to measure which approach (lookup table or switch) is faster. To do this, MeasureThat.net runs each script multiple times and measures the execution time per second for each browser version and device platform listed in the latest benchmark results. **Why Switch Might Be Faster (or Not)** While it's tempting to assume that switches are always slower than lookup tables due to their complexity, there are cases where switches might be faster: 1. **Branch Prediction**: Modern CPUs use branch prediction mechanisms to optimize code execution. If the switch statement is well-ordered and the compiler can predict which branch will be taken, the CPU can execute the corresponding code blocks in parallel. 2. **Constant Folding**: The JavaScript engine can perform constant folding, where it evaluates expressions and replaces them with their results. This might reduce the number of branches in the switch statement, making it faster. However, in general, lookup tables are still likely to be faster because they: 1. **Avoid Branch Misprediction**: Lookup tables avoid branch prediction errors by directly indexing into an array or object. 2. **Reduce Code Size and Overhead**: The code size of a lookup table is typically smaller than that of a switch statement, reducing overhead like function call and return. **Takeaway** In this specific benchmark, the latest results show that the `switch` approach was faster for both browsers, but not significantly so. This might be due to the small input data set used in the test or the fact that the switches are well-ordered and predictable. To confirm the best approach, it's recommended to: 1. Experiment with different input sizes and distributions. 2. Use a profiling tool to measure execution times for specific parts of your code. 3. Optimize your code using techniques like constant folding, loop unrolling, or caching (if applicable). Hope this helps you navigate the world of JavaScript microbenchmarks!
Related benchmarks:
switch vs if-else vs lookup
Switch vs Map object
Map vs switch soh
map vs ifelse vs switch test
object map vs switch 「!!!!」
Comments
Confirm delete:
Do you really want to delete benchmark?