News for hackers. Stuff that matters.
Re:Writing a tiny undo/redo stack in JavaScript by koito17 on Wednesday March 26, 2025 @10:24PM (#43489807)
That works because the length field acts as a fill pointer. In Common Lisp, it's also a common technique to empty an array in-place by setting the fill pointer to 0. The concept is quite old!

In most cases, there isn't a beneficial performance boost from emptying an array this way. One needs to allocate in a loop or do something equally inefficient to measure the difference between `coll = []` and `coll.length = 0`. (This assumes the language runtime does not statically allocate an immutable, empty array and use that to represent all empty arrays. Clojure does this, for instance, so no allocations occur when binding a variable to [] or other empty collections).

Re:Writing a tiny undo/redo stack in JavaScript by normie3000 on Thursday March 27, 2025 @01:04AM (#43490541)
> `coll = []` and `coll.length = 0`

In JavaScript these two statements have very different effects.