-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve performance of
spliceChangesIntoString
(#312)
* Improve performance of spliceChangesIntoString * Add test * Add benchmark * Optimize implementation a bit * Tweak benchmarks * Update changelog --------- Co-authored-by: ABuffSeagull <[email protected]>
- Loading branch information
1 parent
0368ffb
commit 9844623
Showing
4 changed files
with
113 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { bench, describe } from 'vitest' | ||
import type { StringChange } from './types' | ||
import { spliceChangesIntoString } from './utils' | ||
|
||
describe('spliceChangesIntoString', () => { | ||
// 44 bytes | ||
let strTemplate = 'the quick brown fox jumps over the lazy dog ' | ||
let changesTemplate: StringChange[] = [ | ||
{ start: 10, end: 15, before: 'brown', after: 'purple' }, | ||
{ start: 4, end: 9, before: 'quick', after: 'slow' }, | ||
] | ||
|
||
function buildFixture(repeatCount: number, changeCount: number) { | ||
// A large set of changes across random places in the string | ||
let indxes = new Set( | ||
Array.from({ length: changeCount }, (_, i) => | ||
Math.ceil(Math.random() * repeatCount), | ||
), | ||
) | ||
|
||
let changes: StringChange[] = Array.from(indxes).flatMap((idx) => { | ||
return changesTemplate.map((change) => ({ | ||
start: change.start + strTemplate.length * idx, | ||
end: change.end + strTemplate.length * idx, | ||
before: change.before, | ||
after: change.after, | ||
})) | ||
}) | ||
|
||
return [strTemplate.repeat(repeatCount), changes] as const | ||
} | ||
|
||
let [strS, changesS] = buildFixture(5, 2) | ||
bench('small string', () => { | ||
spliceChangesIntoString(strS, changesS) | ||
}) | ||
|
||
let [strM, changesM] = buildFixture(100, 5) | ||
bench('medium string', () => { | ||
spliceChangesIntoString(strM, changesM) | ||
}) | ||
|
||
let [strL, changesL] = buildFixture(1_000, 50) | ||
bench('large string', () => { | ||
spliceChangesIntoString(strL, changesL) | ||
}) | ||
|
||
let [strXL, changesXL] = buildFixture(100_000, 500) | ||
bench('extra large string', () => { | ||
spliceChangesIntoString(strXL, changesXL) | ||
}) | ||
|
||
let [strXL2, changesXL2] = buildFixture(100_000, 5_000) | ||
bench('extra large string (5k changes)', () => { | ||
spliceChangesIntoString(strXL2, changesXL2) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { bench, describe, test } from 'vitest' | ||
import type { StringChange } from './types' | ||
import { spliceChangesIntoString } from './utils' | ||
|
||
describe('spliceChangesIntoString', () => { | ||
test('can apply changes to a string', ({ expect }) => { | ||
let str = 'the quick brown fox jumps over the lazy dog' | ||
let changes: StringChange[] = [ | ||
// | ||
{ start: 10, end: 15, before: 'brown', after: 'purple' }, | ||
] | ||
|
||
expect(spliceChangesIntoString(str, changes)).toBe( | ||
'the quick purple fox jumps over the lazy dog', | ||
) | ||
}) | ||
|
||
test('changes are applied in order', ({ expect }) => { | ||
let str = 'the quick brown fox jumps over the lazy dog' | ||
let changes: StringChange[] = [ | ||
// | ||
{ start: 10, end: 15, before: 'brown', after: 'purple' }, | ||
{ start: 4, end: 9, before: 'quick', after: 'slow' }, | ||
] | ||
|
||
expect(spliceChangesIntoString(str, changes)).toBe( | ||
'the slow purple fox jumps over the lazy dog', | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters