-
-
Notifications
You must be signed in to change notification settings - Fork 123
Native is twice as fast as Wasm
Boris Verkhovskiy edited this page May 6, 2023
·
1 revision
web-tree-sitter (npm install web-tree-sitter
) can run in both the browser and natively, node-tree-sitter (npm install tree-sitter
) can only run natively but it runs faster.
Parsing Python's entire standard library (about 800,000 lines of code) with tree-sitter-python, we get these results on an M1 (Arm) Macbook Air with 8GB of RAM (lower is better):
node-tree-sitter: 2221ms
web-tree-sitter: 5144ms
a 2.3x speedup.
Here's how to reproduce these results:
mkdir /tmp/tree-sitter-vs-node-tree-sitter
cd /tmp/tree-sitter-vs-node-tree-sitter
npm init -y
# add `"type": "module",` to package.json
npm install tree-sitter tree-sitter-cli web-tree-sitter tree-sitter-python
npx tree-sitter build-wasm node_modules/tree-sitter-python
Create a file to parse as a benchmark, for example all files in Python's standard library concatenated into one file:
git clone https://github.com/python/cpython.git
#cd cpython && checkout f508800 && cd ..
find cpython/Lib/ -type f -name "*.py" -exec cat {} + > sample_python_code.py
Create the two JavaScript files that will parse that file in the two possible ways:
native.js
import Parser from "tree-sitter";
import Python from "tree-sitter-python";
import fs from "fs";
const parser = new Parser();
parser.setLanguage(Python);
const file = fs.readFileSync("./sample_python_code.py", "utf8");
const start = process.hrtime.bigint();
parser.parse(file);
const end = process.hrtime.bigint();
console.log(`node-tree-sitter parse time: ${(end - start) / 1000000n}ms`);
wasm.js
import Parser from "web-tree-sitter";
import fs from "fs";
await Parser.init();
const Python = await Parser.Language.load("./tree-sitter-python.wasm");
const parser = new Parser();
parser.setLanguage(Python);
const file = fs.readFileSync("./sample_python_code.py", "utf8");
const start = process.hrtime.bigint();
parser.parse(file);
const end = process.hrtime.bigint();
console.log(`web-tree-sitter parse time: ${(end - start) / 1000000n}ms`);
and run them:
node native.js
node wasm.js