Home
Node.js
function Lookup Table
Updated Dec 13, 2023
Dot Net Perls
Function lookup table. In Node, 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.
Object
function
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.
Tip Instead of having one or many branches (with an if-statement chain) we can just access the function directly.
Tip 2 This approach can speed up applications where many possible branches—many functions—are needed.
// 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); }
FUNCTION B: 100 FUNCTION C: 12 FUNCTION A: 11 FUNCTION B: 100
Benchmark, lookup table. What is the performance of a lookup table of functions? The same assignment statement is executed on the value string (which is set to "dog").
Version 1 We create an object (dict) with 3 string keys and use an anonymous function for each value.
Version 2 We use a string switch and inline all the functions. Each version of the code times its execution.
Result The switch statement on strings is slightly faster than the lookup table of anonymous functions.
var value = "dog"; var y = 0; var dict = { "fish": function() { y = 0; }, "cat": function() { y = 10; }, "dog": function() { y = 20; } } var x1 = performance.now(); // Version 1: call method in dictionary. for (var i = 0; i < 100000000; i++) { dict[value](); } var x2 = performance.now(); // Version 2: use switch to run code based on a string value. for (var i = 0; i < 100000000; i++) { switch (value) { case "fish": y = 0; break; case "cat": y = 10; break; case "dog": y = 20; break; } } var x3 = performance.now(); // Results. console.log("TIME 1: " + (x2 - x1)); console.log("TIME 2: " + (x3 - x2));
TIME 1: 186.135 TIME 2: 185.485
Switch performance. In another test I found that the switch statement is faster than if-statements. Thus it appears in Node.js 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.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 13, 2023 (edit).
Home
Changes
© 2007-2025 Sam Allen