Reducing Name Lookups - J Query

Your code has an inner loop, down in side several levels of nested functions, that runs hundreds or thousands of times. The inner loop calls several global functions, and it references some variables defined in the outer functions or globally.
Each of these references is triggering several name lookups because of the nested functions.It’s slowing down your code, but profilers don’t show what the problem is, and it is n’t obvious from looking at the code that there’s a problem!


Investigate every name that appears in your innermost loop, and figure out how many name lookups it requires. Reduce the number of name lookups by caching object references locally or using fewer nested functions.


Closures are a wonderful thing. They make it trivial to capture state information and pass it along to asynchronous functions such as event handlers or timer callbacks. If JavaScript didn’t have closures, then every asynchronous callback would need to have a way to pass that state around. Instead, you can simply use a nested function.
The dynamic nature of JavaScript is also a wonderful thing. You can add properties and methods to any object, any time, and the JavaScript runtime will happily chase down those references when you need them.
Put these together, and you can get a lot of name lookups.
Consider this code:

Remember that JavaScript looks up a name first in the local scope (function), and if the name isn’t found there, it works its way up through the parent nested functions and finally the global scope. Not only does the JavaScript runtime have to look up each name every time you use it, it also has to repeat those lookups when the names are actually defined in parent functions or in the global scope.
So, if this block of code is in the global scope, the each() callback does the following
name lookups in every iteration:

  1. largest in local scope [fail]
  2. largestin MaxMagnitude() [success]
  3. Math in local scope [fail]
  4. Math in MaxMagnitude() [fail]
  5. Math in anonymous wrapper function [fail]
  6. Math in global scope [success]
  7. absin Math object [success]
  8. Math in local scope [fail]
  9. Math in MaxMagnitude() [fail]
  10. Math in anonymous wrapper function [fail]
  11. Math in global scope [success]
  12. maxin Math object [success]
  13. largest in local scope [fail]
  14. largestin MaxMagnitude() [success]

Now rewrite the code as follows:

This not only eliminates the callback function call in every iteration, it also reduces the number of name lookups per iteration by 10 or more. The loop body in this version does these name lookups:

  1. largest in local scope [success]
  2. abs in local scope [success]
  3. max in local scope [success]
  4. largest in local scope [success]

That’s more than a 70 percent improvement over the first version.
If this code is nested even deeper inside another function, the difference is even greater,since each nested function adds one more lookup for each of the Math object lookups.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

J Query Topics