Function.A bridge crosses the peaceful lake. The scene is beautiful. A gentle breeze causes a ripple in the water. A bridge is a structure that has a function.
Here: We introduce multiplyBy2, a function that receives an argument and returns that value multiplied by 2.
Function argument.We can create an anonymous function, and pass it as an argument to another function. Here we invoke forEach, which loops over the elements of an array.
And: A function that prints the element to the console is called on each iteration.
IIFE example.Sometimes a function must be called once, and right where it is defined. An IIFE an immediately-invoked function expression. It helps us manage scope.
Tip: We have code that is as easy to write as top-level code, but it is in a function. The "animal" variable goes out of scope.
Return.A function can return a value. Often we use if-statements to branch inside a function, and then return the correct value. A return is not required for all functions.
Return, void function.Suppose we try to use the return value of a function, but no return value is reached. We get the special value "undefined."
Right: This is the return value of the function. The value is computed and returned (no "return" keyword is used).
Benchmark, IIFE.Usually an IIFE is best used to isolate a nontrivial piece of code. When we use an IIFE in a hot loop, our code will slow down.
So: Use immediately-invoked function expressions to establish scope and help the garbage collector, not in hot loops.
Result: The two versions of IIFE syntax, with parentheses in slightly different places, incur a cost.
Note: I did not find a consistent improvement in one or the other. So the two IIFE syntax forms are likely equal in performance.
IIFE, notes.In my testing, placing a block of code in an IIFE can improve performance by making globals into locals. But a small cost is incurred to call the surrounding function.
Tip: I suggest using IIFE around "units" of code to establish scope and allow the GC to clean up a page.
Optimization, polymorphic arguments.A function can receive an argument of any type. But if we call a function with varying (polymorphic) argument types, this can thwart optimization.
So: We can improve performance by always calling a function with the same type of argument.
Version 1: The first program calls test() with a string and a number argument. The function can receive either strings or numbers.
Version 2: The second program always calls test() with a string argument. It does not handle numbers directly.
Result: We have better performance when calling a function with non-polymorphic arguments (we always use strings here).
Benchmark: In V8 I found that combining functions tends to increase page load performance—the GC is run less and less allocation is done.
Note: To reproduce this, try timing a complex page with many functions. Then combine them and then time the new version.
Lookup table.We can develop a lookup table of functions. Then we can skip if-statements, and access functions based on a value. This requires some planning.function Lookup
Dot Net Perls
|© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, firstname.lastname@example.org.|