Skip to content

Commit

Permalink
perf: avoiding use of Object.defineProperty in Chunk constructor (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsystancia committed Sep 22, 2022
1 parent 04a05bd commit 130794b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
7 changes: 7 additions & 0 deletions benchmark/index.mjs
Expand Up @@ -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();
15 changes: 10 additions & 5 deletions src/Chunk.js
Expand Up @@ -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) {
Expand Down

0 comments on commit 130794b

Please sign in to comment.