Skip to content

Instantly share code, notes, and snippets.

@favila
Created December 16, 2014 17:56
Show Gist options
  • Save favila/94d5f7e92fa34bdb1461 to your computer and use it in GitHub Desktop.
Save favila/94d5f7e92fa34bdb1461 to your computer and use it in GitHub Desktop.
Function (not macro) to clone javascript arguments object to array in the most jit-friendly way possible.
(defn- arguments-to-array
"This is the most jit-friendly way to turn arguments into an array.
js-this is a uint32 start index.
Call *exactly* like so: (.apply arguments-to-array start-index (js-arguments))
Any other call pattern will prevent optimizations in calling function.
Returns a real js array of its arguments starting from start-index.
@param {...*} var_args"
[var_args]
(let [len (alength (js-arguments))
a #js []]
(loop [i 0 j (js-this)]
(if (< j len)
(do
(aset a i (aget (js-arguments) j))
(recur (inc i) (inc j)))
a))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment