add vec as PersistentVector.fromArray #198
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was running some performance tests on mori vs. immutable, and found that we could make vector creation from a JS array at least two times faster:
https://gist.github.com/edge/571bc3da31cb37d98addd78cc937f4d6
The idiomatic way to create a vector with 100 zeroes (yielding 113 ms):
A slightly more efficient way (yielding 56 ms):
However, this approach is limited to the number of arguments per call that the Javascript engine supports (though still quite a high number).
The
arguments
object is actually quite slow in Javascript, and this is well documented.By skipping certain checks when we know that we are instantiating from an array, we can eschew the expensive conversion (
[& args]
), avoid the function call limitation, and provide a cleaner interface (yielding 28 ms):I also experimented with using
cljs.core/vec
, but found it even slower than the apply strategy, perhaps because PersistentVector.fromArray is that much more efficient.