From 130794bb8bfd9f21eb1f50c36a1da8eb5443d256 Mon Sep 17 00:00:00 2001 From: rsystancia <110410059+rsystancia@users.noreply.github.com> Date: Thu, 22 Sep 2022 10:21:29 +0200 Subject: [PATCH] perf: avoiding use of Object.defineProperty in Chunk constructor (#219) --- benchmark/index.mjs | 7 +++++++ src/Chunk.js | 15 ++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/benchmark/index.mjs b/benchmark/index.mjs index 1898a7a..df606ff 100644 --- a/benchmark/index.mjs +++ b/benchmark/index.mjs @@ -75,6 +75,13 @@ async function bench() { }, s => { s.replace(/replacement/g, 'replacement\nReplacement'); }); + + const size = 1000000; + runWithInstance('overwrite', ['a'.repeat(size)], s => { + for (let i = 1; i < size; i+=2) { + s.overwrite(i, i+1, 'b'); + } + }); } bench(); diff --git a/src/Chunk.js b/src/Chunk.js index 53ce9a5..e15cc6a 100644 --- a/src/Chunk.js +++ b/src/Chunk.js @@ -11,11 +11,16 @@ export default class Chunk { this.storeName = false; this.edited = false; - // we make these non-enumerable, for sanity while debugging - Object.defineProperties(this, { - previous: { writable: true, value: null }, - next: { writable: true, value: null }, - }); + if (DEBUG) { + // we make these non-enumerable, for sanity while debugging + Object.defineProperties(this, { + previous: { writable: true, value: null }, + next: { writable: true, value: null }, + }); + } else { + this.previous = null; + this.next = null; + } } appendLeft(content) {