Created
December 16, 2014 17:56
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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