Skip to content

Commit

Permalink
feat: perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cyyynthia committed May 6, 2023
1 parent 6d364af commit bd69ffb
Show file tree
Hide file tree
Showing 18 changed files with 48,984 additions and 104 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ smol-toml only fails to comply with the spec in 2 (minor) ways:
TODO

## Performance
TODO
A note on these performance numbers: in some highly synthetic tests, other parsers such as `fast-toml` greatly
outperform other parsers, mostly due to their lack of compliance with the spec. For example, to parse a string,
`fast-toml` skips the entire string while `smol-toml` does validate the string, costing a fair chair of performance.

The ~5MB test file used for benchmark here is filled with random data which attempts to be close-ish to reality. The
idea is to have a file relatively close to a real-world application.

The large TOML generator can be found [here](https://gist.github.com/cyyynthia/e77c744cb6494dabe37d0182506526b9)

(spoiler: it's fast)
| | smol-toml | @iarna/toml@3.0.0 | @ltd/j-toml | fast-toml |
|----------------|---------------------|-------------------|----------------|----------------|
| Spec example | **34,928.57 op/s** | 21,322.81 op/s | 12,472.90 op/s | 28,358.79 op/s |
| ~5MB test file | **4.1308 op/s** | *DNF* | 2.1595 op/s | 2.2635 op/s |
47 changes: 47 additions & 0 deletions bench/parse.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { beforeAll, describe, bench } from 'vitest'
import { readFile } from 'fs/promises'
import { parse as smolTomlParse } from '../src/index.js'
import { parse as iarnaTomlParse } from '@iarna/toml'
import { parse as ltdJTomlParse } from '@ltd/j-toml'
import fastTomlParse from 'fast-toml'

describe('TOML spec example', async () => {
let toml = await readFile(new URL('./testfiles/toml-spec-example.toml', import.meta.url), 'utf8')

bench('smol-toml', () => {
smolTomlParse(toml)
})

bench('@iarna/toml', () => {
iarnaTomlParse(toml)
})

bench('@ltd/j-toml', () => {
ltdJTomlParse(toml)
})

bench('fast-toml', () => {
fastTomlParse(toml)
})
})

describe('5MB of TOML (all structures)', async () => {
let toml = await readFile(new URL('./testfiles/5mb-mixed.toml', import.meta.url), 'utf8')

bench('smol-toml', () => {
smolTomlParse(toml)
})

// DNF
// bench('@iarna/toml', () => {
// iarnaTomlParse(toml)
// })

bench('@ltd/j-toml', () => {
ltdJTomlParse(toml, { joiner: '\n' })
})

bench('fast-toml', () => {
fastTomlParse(toml)
})
})
48,552 changes: 48,552 additions & 0 deletions bench/testfiles/5mb-mixed.toml

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions bench/testfiles/toml-spec-example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This is a TOML document

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00

[database]
enabled = true
ports = [ 8000, 8001, 8002 ]
data = [ ["delta", "phi"], [3.14] ]
temp_targets = { cpu = 79.5, case = 72.0 }

[servers]

[servers.alpha]
ip = "10.0.0.1"
role = "frontend"

[servers.beta]
ip = "10.0.0.2"
role = "backend"
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"repository": "[email protected]:squirrelchat/smol-toml.git",
"author": "Cynthia <[email protected]>",
"license": "BSD-3-Clause",
"type": "module",
"engines": {
"node": ">= 18",
"pnpm": ">= 7.24"
Expand All @@ -17,13 +18,16 @@
"test": "vitest"
},
"devDependencies": {
"@iarna/toml": "3.0.0",
"@ltd/j-toml": "^1.38.0",
"@tsconfig/esm": "^1.0.3",
"@tsconfig/node-lts": "^18.12.1",
"@tsconfig/strictest": "^2.0.1",
"@types/node": "^18.16.3",
"@vitest/ui": "^0.30.1",
"@types/node": "^20.1.0",
"@vitest/ui": "^0.31.0",
"fast-toml": "^0.5.4",
"typescript": "^5.0.4",
"vitest": "^0.30.1"
"vitest": "^0.31.0"
},
"exports": {
".": {
Expand Down
Loading

0 comments on commit bd69ffb

Please sign in to comment.