Context
Developers often curios about how Node.js ports of OpenAI's tiktoken perform.
This question is usually prompted by Vercel's Edge Runtime and AWS Lambda requiring more work to make ports with Python binding and WASM to work compared to the purely JavaScript js-tiktoken
.
Benchmark results
$ npm start
> tiktoken-benchmark@1.0.0 start
> node index.js
Fastest is @dqbd/tiktoken with 1992 ops/sec
Slowest is js-tiktoken with 1494 ops/sec (75%)
With short completion content:
Fastest is js-tiktoken with 31334 ops/sec
Slowest is @dqbd/tiktoken with 5460 ops/sec (17%)
Content:
Once upon a time, in a small village nestled among rolling green hills, lived a young girl named Lily. She possessed an insatiable curiosity and an adventurous spirit that often led her to explore the world beyond her home.
Libraries
- @dqbd/tiktoken uses WASM
- js-tiktoken is pure JavaScript
Environment
MB Air Apple M1 Node.js v18.18.0
Related blog post: https://openmeter.io/blog/token-usage-with-openai-streams-and-nextjs
Moving the initializer inside the test has a big impact:
Fastest is @dqbd/tiktoken with 20 ops/sec
Slowest is js-tiktoken with 10 ops/sec (52%)