Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add quickjs benchmark #62

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ lcov.info

*.hermes.js
*.hermes.hbc
*.bundle.js
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ Here is a brief explanation, and you can see [archived benchmark results](benchm

**Performance in React Native**: `unicode-segmenter/grapheme` is significantly faster than alternatives when compiled to Hermes bytecode. It's 3\~8x faster than `graphemer` and 20\~26x faster than `grapheme-splitter`, with the performance gap increasing with input size.

**Performance in QuickJS**: `unicode-segmenter/grapheme` is the only usable library in terms of performance.

Instead of trusting these claims, you can try `yarn perf:grapheme` directly in your environment or build your own benchmark.

## LICENSE
Expand Down
45 changes: 45 additions & 0 deletions benchmark/grapheme/perf-quickjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as path from 'node:path';
import { build } from 'esbuild';
import { $ } from 'zx';

let baseDir = import.meta.dirname;

let libs = [
['unicode-segmenter/grapheme', 'bundle-entries/unicode-segmenter.js', 'perf-quickjs/unicode-segmenter.js'],
['graphemer', 'bundle-entries/graphemer.js', 'perf-quickjs/graphemer.js'],
['grapheme-splitter', 'bundle-entries/grapheme-splitter.js', 'perf-quickjs/grapheme-splitter.js'],
];

let benches = [];

for (let lib of libs) {
let libName = lib[0];
let libEntry = path.join(baseDir, lib[1]);
let execEntry = path.join(baseDir, lib[2]);
let bundleEntry = libEntry.replace(/\.js$/, '.bundle.js');

await build({
write: true,
bundle: true,
minify: true,
format: 'esm',
entryPoints: [libEntry],
outfile: bundleEntry,
});

benches.push({
libName,
libEntry,
execEntry,
bundleEntry,
});
}

console.log('\nExecuting QuickJS benchmark...\n');

let args = process.argv.slice(2).join(' ');

for (let bench of benches) {
console.log(`--- ${bench.libName} ---\n`);
await $({ stdio: 'inherit' })`qjs ${args} ${bench.execEntry}`;
}
28 changes: 28 additions & 0 deletions benchmark/grapheme/perf-quickjs/grapheme-splitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { inputs, simpleBench } from '../../_simple-bench.js';

import { GraphemeSplitter } from '../bundle-entries/grapheme-splitter.bundle.js';

let graphemeSplitter = new (GraphemeSplitter.default || GraphemeSplitter)();

{
let result = simpleBench(1000, () => {
void [...graphemeSplitter.iterateGraphemes(inputs.small)];
});

print(`grapheme-splitter (small input)`);
print(`samples: ${result.samples}`);
print(`duration (avg): ${result.avgDuration}`);
print();
}


{
let result = simpleBench(1000, () => {
void [...graphemeSplitter.iterateGraphemes(inputs.medium)];
});

print(`grapheme-splitter (medium input)`);
print(`samples: ${result.samples}`);
print(`duration (avg): ${result.avgDuration}`);
print();
}
28 changes: 28 additions & 0 deletions benchmark/grapheme/perf-quickjs/graphemer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { inputs, simpleBench } from '../../_simple-bench.js';

import { Graphemer } from '../bundle-entries/graphemer.bundle.js';

let graphemer = new (Graphemer.default || Graphemer)();

{
let result = simpleBench(1000, () => {
void [...graphemer.iterateGraphemes(inputs.small)];
});

print(`graphemer (small input)`);
print(`samples: ${result.samples}`);
print(`duration (avg): ${result.avgDuration}`);
print();
}


{
let result = simpleBench(1000, () => {
void [...graphemer.iterateGraphemes(inputs.medium)];
});

print(`graphemer (medium input)`);
print(`samples: ${result.samples}`);
print(`duration (avg): ${result.avgDuration}`);
print();
}
25 changes: 25 additions & 0 deletions benchmark/grapheme/perf-quickjs/unicode-segmenter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { inputs, simpleBench } from '../../_simple-bench.js';

import { graphemeSegments } from '../bundle-entries/unicode-segmenter.js';

{
let result = simpleBench(1000, () => {
void [...graphemeSegments(inputs.small)];
});

print(`unicode-segmenter/grapheme (small input)`);
print(`samples: ${result.samples}`);
print(`duration (avg): ${result.avgDuration}`);
print();
}

{
let result = simpleBench(1000, () => {
void [...graphemeSegments(inputs.medium)];
});

print(`unicode-segmenter/grapheme (medium input)`);
print(`samples: ${result.samples}`);
print(`medium input (avg): ${result.avgDuration}`);
print();
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@
"perf:general": "node benchmark/general/perf.js",
"perf:grapheme": "node benchmark/grapheme/perf.js",
"perf:grapheme:browser": "vite benchmark/grapheme",
"perf:grapheme:hermes": "node benchmark/grapheme/perf-hermes.js"
"perf:grapheme:hermes": "node benchmark/grapheme/perf-hermes.js",
"perf:grapheme:quickjs": "node benchmark/grapheme/perf-quickjs.js"
},
"alias": {
"process": false
Expand Down