Artur Malabarba points to this excellent Stack Exchange entry on the speed of let with lexical versus dynamic scope. Malabarba asks why let
is faster with lexical scope than it is with dynamic scope. lunaryorn provides an excellent and detailed answer that shows the generated byte code for both cases.
The TL;DR is that using dynamic scope means that the let
variables have to be looked up in the global scope, set, and then reset after use, while using lexical scope just makes the variables local and avoids all lookup and setting/resetting. That may sound a little opaque but lunaryord’s answer explains things in a very understandable way.
Generally, I don’t worry too much about speed in the Elisp I write because it’s mostly just simple functions that run quickly no matter how ham handed my coding is. If you write functions that have “long” running times, it’s worthwhile to take the lessons in lunaryord’s answer into account. It is, in any event interesting and worth knowing for the day you need it.