| ▲ | spacechild1 8 months ago | |||||||||||||||||||||||||||||||
I don't think there is much pointer chasing at runtime. With lexically scoped closures it's only the compiler who walks the stack frames to find the referenced variable; the compiled function can point directly to the object in the stack frame. In my understanding, closed over variables have (almost) no runtime cost over "normal" local variables. Please correct me if I'm wrong. | ||||||||||||||||||||||||||||||||
| ▲ | nmilo 8 months ago | parent [-] | |||||||||||||||||||||||||||||||
I meant more like storing closures to be used later after any locals are out of the stack frame, but tbh that's an abstraction that also causes allocations in C++ and Rust. On the other hand, no idea how JS internals work but I know in python getting the length of an array takes five layers of pointer indirection so it very well could be pointer to closure object -> pointer to list of closed variables -> pointer to boxed variable -> pointer to number or some ridiculous thing like that. | ||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||