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)}
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");
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lookup | |
switch |
Test name | Executions per second |
---|---|
lookup | 451355.0 Ops/sec |
switch | 2911729.2 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition
The benchmark is defined as follows:
"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:
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.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:
However, in general, lookup tables are still likely to be faster because they:
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:
Hope this helps you navigate the world of JavaScript microbenchmarks!