Skip to content

Commit

Permalink
chore: update benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 2, 2025
1 parent 49cbfd7 commit 73cbddd
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 42 deletions.
33 changes: 8 additions & 25 deletions bundle-test/bench.mjs → bench/bundle-test/bundle.bench.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable antfu/no-import-dist */
/* eslint-disable no-console */

import { bench, describe } from 'vitest'
import { highlight as highlightA } from './dist/index-lite.min.mjs'

Check failure on line 4 in bench/bundle-test/bundle.bench.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module './dist/index-lite.min.mjs' or its corresponding type declarations.
import { highlight as highlightB } from './dist/index-wasm.min.mjs'

Check failure on line 5 in bench/bundle-test/bundle.bench.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module './dist/index-wasm.min.mjs' or its corresponding type declarations.

Expand All @@ -19,30 +20,12 @@ function notify() {
}
`

async function run() {
// Warn up
for (let i = 0; i < 200; i++) {
await highlightA(code)
await highlightB(code)
}

let tA = 0
let tB = 0

const startA = performance.now()
for (let i = 0; i < 1000; i++) {
describe('bundle', () => {
bench('js-precompiled', async () => {
await highlightA(code)
}
tA += performance.now() - startA
})

const startB = performance.now()
for (let i = 0; i < 1000; i++) {
bench('wasm', async () => {
await highlightB(code)
}
tB += performance.now() - startB

console.log('A', tA)
console.log('B', tB)
}

run()
})
})
4 changes: 2 additions & 2 deletions bundle-test/index-lite.ts → bench/bundle-test/index-lite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const shiki = createShikiInternal(
import('@shikijs/langs-precompiled/ts'),
],
themes: [
import('@shikijs/themes/nord'),
import('@shikijs/themes/vitesse-dark'),
],
engine: createJavaScriptRawEngine(),
},
)

export async function highlight(code: string): Promise<string> {
return codeToHtml(await shiki, code, { lang: 'ts', theme: 'nord' })
return codeToHtml(await shiki, code, { lang: 'ts', theme: 'vitesse-dark' })
}
4 changes: 2 additions & 2 deletions bundle-test/index-wasm.ts → bench/bundle-test/index-wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const shiki = createShikiInternal(
import('@shikijs/langs/ts'),
],
themes: [
import('@shikijs/themes/nord'),
import('@shikijs/themes/vitesse-dark'),
],
engine: createWasmOnigEngine(import('@shikijs/engine-oniguruma/wasm-inlined')),
},
)

export async function highlight(code: string): Promise<string> {
return codeToHtml(await shiki, code, { lang: 'ts', theme: 'nord' })
return codeToHtml(await shiki, code, { lang: 'ts', theme: 'vitesse-dark' })
}
6 changes: 6 additions & 0 deletions bench/bundle-test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"scripts": {
"bench:prepare": "rollup -c && du -h dist/*"
}
}
File renamed without changes.
26 changes: 20 additions & 6 deletions bench/engines.bench.ts → bench/engines/engines.bench.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/* eslint-disable no-console */
import type { BundledLanguage } from 'shiki'
import type { ReportItem } from '../scripts/report-engine-js-compat'
import type { ReportItem } from '../../scripts/report-engine-js-compat'
import fs from 'node:fs/promises'
import { createHighlighter, createJavaScriptRegexEngine, createOnigurumaEngine } from 'shiki'
import { createJavaScriptRawEngine, createJavaScriptRegexEngine } from '@shikijs/engine-javascript'
import { createHighlighter, createOnigurumaEngine } from 'shiki'
import { bench, describe } from 'vitest'

const js = createJavaScriptRegexEngine()
const jsRaw = createJavaScriptRawEngine()
const wasm = await createOnigurumaEngine(() => import('shiki/wasm'))

const RANGE = [0, 20]

// Run `npx jiti scripts/report-engine-js-compat.ts` to generate the report first
const report = await fs.readFile(new URL('../scripts/report-engine-js-compat.json', import.meta.url), 'utf-8').then(JSON.parse) as ReportItem[]
const report = await fs.readFile(new URL('../../scripts/report-engine-js-compat.json', import.meta.url), 'utf-8').then(JSON.parse) as ReportItem[]
const langs = report.filter(i => i.highlightMatch === true).map(i => i.lang).slice(...RANGE) as BundledLanguage[]
// Clone https://github.com/shikijs/textmate-grammars-themes to `../tm-grammars-themes`
const samples = await Promise.all(langs.map(lang => fs.readFile(`../tm-grammars-themes/samples/${lang}.sample`, 'utf-8')))
const samples = await Promise.all(langs.map(lang => fs.readFile(new URL(`../../tm-grammars-themes/samples/${lang}.sample`, import.meta.url), 'utf-8')))

console.log('Benchmarking engines with', langs.length, 'languages')

Expand All @@ -30,14 +32,26 @@ const shikiWasm = await createHighlighter({
engine: wasm,
})

const shikiJsPrecompiled = await createHighlighter({
langs: await Promise.all(langs.map(lang => import(`@shikijs/langs-precompiled/${lang}`))),
themes: ['vitesse-dark'],
engine: jsRaw,
})

for (const lang of langs) {
describe(lang, () => {
const code = samples[langs.indexOf(lang)]

bench('js', () => {
shikiJs.codeToTokensBase(samples[langs.indexOf(lang)], { lang, theme: 'vitesse-dark' })
shikiJs.codeToTokensBase(code, { lang, theme: 'vitesse-dark' })
})

bench('js-precompiled', () => {
shikiJsPrecompiled.codeToTokensBase(code, { lang, theme: 'vitesse-dark' })
})

bench('wasm', () => {
shikiWasm.codeToTokensBase(samples[langs.indexOf(lang)], { lang, theme: 'vitesse-dark' })
shikiWasm.codeToTokensBase(code, { lang, theme: 'vitesse-dark' })
})
})
}
6 changes: 0 additions & 6 deletions bundle-test/package.json

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"docs": "pnpm -C docs run docs:dev",
"docs:build": "pnpm -C docs run docs:build",
"report-engine-js": "esno scripts/report-engine-js-compat.ts",
"bench": "vitest bench",
"bench": "pnpm -r run bench:prepare && vitest bench",
"prepare": "simple-git-hooks"
},
"devDependencies": {
Expand Down

0 comments on commit 73cbddd

Please sign in to comment.