Welcome! Please see the
About
page for a little more info on how this works.
GitHub Login
All Activity
Questions
Unanswered
Tags
Ask a Question
About
Ask a Question
ES6 Iterators should use IIterable if possible
0
votes
asked
Jan 15, 2018
in
ClojureScript
by
jira
Motivation:
ES6 Iterables:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
Use in React:
https://github.com/facebook/react/blob/30dac4e78de02fb427ee82013160ae875128d7a2/packages/react/src/ReactElementValidator.js#L195-L204
(defn es6-iterator**
[coll]
(if (implements? IIterable coll)
(let [it (-iterator ^not-native coll)]
#js{:next (fn []
(if ^boolean (.hasNext it)
#js{:value (.next it), :done false}
#js{:value nil, :done true}))})
;; Fallback to naive first/next iterator:
(ES6Iterator. (seq coll))))
;; Tests can use round trip:
(es6-iterator-seq (es6-iterator (hash-map 0 1 2 3)))
(defn es6-iter-consume
[it]
(while (not (.-done (.next it)))))
(dotimes [_ 3]
(let [xs (vec (range 3000))
runs 1000]
(simple-benchmark []
(es6-iter-consume (es6-iterator xs)) runs)
(simple-benchmark []
(es6-iter-consume (es6-iterator** xs)) runs)))
About a 4x speedup in Chrome. Also note that *much* less garbage is produced.
request
jira
Please
log in
or
register
to add a comment.
Please
log in
or
register
to answer this question.
1
Answer
0
votes
answered
Jun 26, 2019
by
jira
Reference:
https://clojure.atlassian.net/browse/CLJS-2472
(reported by aralo)
Please
log in
or
register
to add a comment.
Welcome to Clojure Q&A, where you can ask questions and receive answers from members of the Clojure community.
Related questions
Collection hashers should use iterators
random-uuid should use a cryptographically strong PRNG if available
Use transients with select-keys if possible
Terrible error message if I try to use :refer :all
enhance numeric inference, if + number? test on local var should tag local var in the successful branch
Categories
All categories
Clojure
(1.5k)
ClojureScript
(612)
ClojureCLR
(26)
Contrib libs
(726)
Tools
(74)
Meta
(19)
Other
(93)
Beginner
(0)
...