From 11bb8f03c2cbe4c6d7691b9b4494d1254bc7b829 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Tue, 26 Mar 2019 12:22:47 +0000 Subject: [PATCH] Remove string allocation from try_iter This allows to significantly speed up iteration over small collections, where string encoding is the primary overhead. Related to #1386, but works around only this partial case. --- crates/js-sys/src/lib.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 0a51ec7dc35..3a9f44e99bf 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -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, 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() { @@ -1113,8 +1121,7 @@ pub fn try_iter(val: &JsValue) -> Result, JsValue> { return Ok(None); } - let next = JsValue::from("next"); - let next = Reflect::get(&it, &next)?; + let next = it.unchecked_ref::().next(); Ok(if next.is_function() { let it: Iterator = it.unchecked_into();