Skip to content

Commit

Permalink
Add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed Aug 13, 2024
1 parent 9ed705d commit fdbe377
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/utils.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 [strSmall, changesSmall] = buildFixture(10, 5)
bench('small string', () => {
spliceChangesIntoString(strSmall, changesSmall)
})

let [strMedium, changesMedium] = buildFixture(1_000, 50)
bench('medium string', () => {
spliceChangesIntoString(strMedium, changesMedium)
})

let [strLarge, changesLarge] = buildFixture(100_000, 7_000)
bench('large string', () => {
spliceChangesIntoString(strLarge, changesLarge)
})
})

0 comments on commit fdbe377

Please sign in to comment.