Skip to content

Commit

Permalink
Remove string allocation from try_iter
Browse files Browse the repository at this point in the history
This allows to significantly speed up iteration over small collections, where string encoding is the primary overhead.

Related to rustwasm#1386, but works around only this partial case.
  • Loading branch information
RReverser committed Mar 26, 2019
1 parent b4b3926 commit 11bb8f0
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,14 @@ impl IterState {
/// Create an iterator over `val` using the JS iteration protocol and
/// `Symbol.iterator`.
pub fn try_iter(val: &JsValue) -> Result<Option<IntoIter>, JsValue> {
#[wasm_bindgen]
extern "C" {
type MaybeIterator;

#[wasm_bindgen(method, getter)]
fn next(this: &MaybeIterator) -> JsValue;
}

let iter_sym = Symbol::iterator();
let iter_fn = Reflect::get(val, iter_sym.as_ref())?;
if !iter_fn.is_function() {
Expand All @@ -1113,8 +1121,7 @@ pub fn try_iter(val: &JsValue) -> Result<Option<IntoIter>, JsValue> {
return Ok(None);
}

let next = JsValue::from("next");
let next = Reflect::get(&it, &next)?;
let next = it.unchecked_ref::<MaybeIterator>().next();

Ok(if next.is_function() {
let it: Iterator = it.unchecked_into();
Expand Down

0 comments on commit 11bb8f0

Please sign in to comment.