@@ -123,15 +123,23 @@ impl InternalBinaryNode {
123123
124124 #[ inline]
125125 fn convert_attrs ( attrs : & [ ( Cow < ' _ , str > , ValueRef < ' _ > ) ] ) -> Attrs {
126- let obj = Object :: new ( ) ;
127- for ( k, v) in attrs. iter ( ) {
128- let js_value = match v. as_str ( ) {
129- Some ( s) => JsValue :: from_str ( s) ,
130- None => JsValue :: from_str ( & v. to_string ( ) ) ,
131- } ;
132- let _ = js_sys:: Reflect :: set ( & obj, & JsValue :: from_str ( k) , & js_value) ;
126+ // Use Object.from_entries() which is faster than multiple Reflect.set() calls
127+ let entries = Array :: new_with_length ( attrs. len ( ) as u32 ) ;
128+ for ( i, ( k, v) ) in attrs. iter ( ) . enumerate ( ) {
129+ let pair = Array :: new_with_length ( 2 ) ;
130+ pair. set ( 0 , JsValue :: from_str ( k) ) ;
131+ pair. set (
132+ 1 ,
133+ match v. as_str ( ) {
134+ Some ( s) => JsValue :: from_str ( s) ,
135+ None => JsValue :: from_str ( & v. to_string ( ) ) ,
136+ } ,
137+ ) ;
138+ entries. set ( i as u32 , pair. into ( ) ) ;
133139 }
134- obj. unchecked_into ( )
140+ Object :: from_entries ( & entries)
141+ . unwrap_or_else ( |_| Object :: new ( ) )
142+ . unchecked_into ( )
135143 }
136144}
137145
@@ -146,12 +154,17 @@ impl InternalBinaryNode {
146154 pub fn to_json ( & self ) -> JsValue {
147155 let obj = Object :: new ( ) ;
148156
157+ // Use interned strings for frequently used keys
149158 let _ = js_sys:: Reflect :: set (
150159 & obj,
151- & JsValue :: from_str ( "tag" ) ,
160+ & wasm_bindgen :: intern ( "tag" ) . into ( ) ,
152161 & JsValue :: from_str ( & self . node_ref ( ) . tag ) ,
153162 ) ;
154- let _ = js_sys:: Reflect :: set ( & obj, & JsValue :: from_str ( "attrs" ) , & self . attrs ( ) . into ( ) ) ;
163+ let _ = js_sys:: Reflect :: set (
164+ & obj,
165+ & wasm_bindgen:: intern ( "attrs" ) . into ( ) ,
166+ & self . attrs ( ) . into ( ) ,
167+ ) ;
155168
156169 if let Some ( content) = self . content ( ) {
157170 let content_js: JsValue = content. into ( ) ;
@@ -160,7 +173,11 @@ impl InternalBinaryNode {
160173 } else {
161174 content_js
162175 } ;
163- let _ = js_sys:: Reflect :: set ( & obj, & JsValue :: from_str ( "content" ) , & content_value) ;
176+ let _ = js_sys:: Reflect :: set (
177+ & obj,
178+ & wasm_bindgen:: intern ( "content" ) . into ( ) ,
179+ & content_value,
180+ ) ;
164181 }
165182
166183 obj. into ( )
@@ -169,7 +186,8 @@ impl InternalBinaryNode {
169186 fn serialize_child_nodes ( & self , content_js : & JsValue ) -> JsValue {
170187 let arr = Array :: from ( content_js) ;
171188 let json_arr = Array :: new_with_length ( arr. length ( ) ) ;
172- let to_json_key = JsValue :: from_str ( "toJSON" ) ;
189+ // Use interned string for toJSON key - called repeatedly
190+ let to_json_key: JsValue = wasm_bindgen:: intern ( "toJSON" ) . into ( ) ;
173191
174192 for i in 0 ..arr. length ( ) {
175193 let item = arr. get ( i) ;
0 commit comments