diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-03-19 21:31:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-19 21:31:56 +0100 |
commit | 87d2ba42bf0dedcd91059145bf8ab5941236354b (patch) | |
tree | 52ad0906f8b41586cf9fb57ce02646100725b0c6 /core/bindings.rs | |
parent | 392d2c11182332b8d3c168169b1585e3419cb1eb (diff) |
perf: Optimize TextEncoder and TextDecoder (#4430)
* add tests for "Deno.core.encode" and "Deno.core.decode" for empty inputs
* use "Deno.core.encode" in "TextEncoder"
* use "Deno.core.decode" in "TextDecoder"
* remove "core_decode" and "core_encode" benchmarks
Diffstat (limited to 'core/bindings.rs')
-rw-r--r-- | core/bindings.rs | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/core/bindings.rs b/core/bindings.rs index 3745abf69..88bdf7f30 100644 --- a/core/bindings.rs +++ b/core/bindings.rs @@ -632,7 +632,20 @@ fn encode( }; let text_str = text.to_rust_string_lossy(scope); let text_bytes = text_str.as_bytes().to_vec().into_boxed_slice(); - let buf = boxed_slice_to_uint8array(scope, text_bytes); + + let buf = if text_bytes.is_empty() { + let ab = v8::ArrayBuffer::new(scope, 0); + v8::Uint8Array::new(ab, 0, 0).expect("Failed to create UintArray8") + } else { + let buf_len = text_bytes.len(); + let backing_store = + v8::ArrayBuffer::new_backing_store_from_boxed_slice(text_bytes); + let mut backing_store_shared = backing_store.make_shared(); + let ab = + v8::ArrayBuffer::with_backing_store(scope, &mut backing_store_shared); + v8::Uint8Array::new(ab, 0, buf_len).expect("Failed to create UintArray8") + }; + rv.set(buf.into()) } |