JavaScript function Lookup Table

Create an object with function values. Perform lookups on the table to reduce branches.
Function lookup table. A switch statement can run a block of code when a value equals a specific string. But an object with function values can do the same thing.
In this benchmark, we test an object with function values. Each function is the value for a string key. We then invoke a method by its key.Objectfunction
Lookup table example. A lookup table of functions lets us encode branches in memory. We put a function object in each index of an array.Array

Then: We invoke functions based on an array element access. We simply call the array element as a function.

So: Instead of having one or many branches (with an if-statement chain) we can just access the function directly.

Tip: This approach can speed up applications where many possible branches—many functions—are needed.

JavaScript program that calls lookup table functions // A function lookup table. var data = [function(value) { console.log("FUNCTION A: " + (value + 1)); }, function(value) { console.log("FUNCTION B: " + (value * 10)); }, function(value) { console.log("FUNCTION C: " + (value + 2)); }]; // The indexes of the functions we wish to call. var calls = [1, 2, 0, 1]; // Loop through the call numbers and invoke the functions in the table. for (var i = 0; i < calls.length; i++) { data[calls[i]](10); } Output FUNCTION B: 100 FUNCTION C: 12 FUNCTION A: 11 FUNCTION B: 100
Benchmark, lookup table. Both of these programs have the same effect. The same assignment statement is executed on the value string (which is set to "dog").

First: We create an object (dict) with 3 string keys and use an anonymous function for each value.

Second: We use a string switch and inline all the functions. Each program times its execution.

JavaScript program that uses lookup table, functions // Use dictionary of functions to run code. var dict = { "fish": function() { var y = 0; }, "cat": function() { var y = 10; }, "dog": function() { var y = 20; } } var x1 =; var value = "dog"; for (var i = 0; i < 1000000; i++) { // Call method in dictionary. dict[value](); } var x2 =; console.log("TIME: " + (x2 - x1)); JavaScript program that uses string switch var x1 =; var y = 0; var value = "dog"; for (var i = 0; i < 1000000; i++) { // Use switch to run code based on a string value. switch (value) { case "fish": y = 0; break; case "cat": y = 10; break; case "dog": y = 20; break; } } var x2 =; console.log("TIME: " + (x2 - x1)); Output 4.63 ms lookup table, functions 4.45 ms switch
Result, notes. The switch statement on strings is slightly faster than the lookup table of anonymous functions. Calling a function has some extra instructions.
Switch performance. In another test I found that the switch statement is faster than if-statements. Thus it appears in JavaScript a switch is the best option for max performance.Switch
Some considerations. A lookup table may lead to clearer code, and often lookup time is not an important part of web application performance. But on a low level, the switch appears superior.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls