Gathering detailed insights and metrics for subscript
Gathering detailed insights and metrics for subscript
Gathering detailed insights and metrics for subscript
Gathering detailed insights and metrics for subscript
npm install subscript
Typescript
Module System
Node Version
NPM Version
JavaScript (99.41%)
HTML (0.59%)
Total Downloads
46,902
Last Day
568
Last Week
2,349
Last Month
6,983
Last Year
32,884
ISC License
126 Stars
1,009 Commits
4 Forks
6 Watchers
1 Branches
1 Contributors
Updated on May 07, 2025
Minified
Minified + Gzipped
Latest Version
9.1.0
Package Id
subscript@9.1.0
Unpacked Size
46.12 kB
Size
15.60 kB
File Count
36
NPM Version
10.9.2
Node Version
23.7.0
Published on
Feb 20, 2025
Cumulative downloads
Total Downloads
Last Day
79.2%
568
Compared to previous day
Last Week
34%
2,349
Compared to previous week
Last Month
-14.2%
6,983
Compared to previous month
Last Year
482.3%
32,884
Compared to previous year
Subscript is fast, tiny & extensible parser / evaluator / microlanguage with standard syntax.
Subscript has 3.5kb footprint (compare to 11.4kb jsep + 4.5kb expression-eval), best performance and extensive test coverage.
1import subscript from './subscript.js' 2 3// parse expression 4const fn = subscript('a.b + Math.sqrt(c - 1)') 5 6// evaluate with context 7fn({ a: { b:1 }, c: 5, Math }) 8// 3
Subscript supports common syntax (JavaScript, C, C++, Java, C#, PHP, Swift, Objective-C, Kotlin, Perl etc.):
a.b
, a[b]
, a(b)
a++
, a--
, ++a
, --a
a * b
, a / b
, a % b
+a
, -a
, a + b
, a - b
a < b
, a <= b
, a > b
, a >= b
, a == b
, a != b
~a
, a & b
, a ^ b
, a | b
, a << b
, a >> b
!a
, a && b
, a || b
a = b
, a += b
, a -= b
, a *= b
, a /= b
, a %= b
, a <<= b
, a >>= b
(a, (b))
, a; b;
"abc"
, 'abc'
0.1
, 1.2e+3
Just-in is no-keywords JS subset, JSON + expressions (see thread).
It extends subscript with:
a === b
, a !== b
a ** b
, a **= b
a ?? b
, a ??= b
a ||= b
, a &&= b
a >>> b
, a >>>= b
a ? b : c
, a?.b
...a
[a, b]
{a: b}
(a, b) => c
// foo
, /* bar */
true
, false
, null
, NaN
, undefined
a in b
1import jstin from './justin.js' 2 3let xy = jstin('{ x: 1, "y": 2+2 }["x"]') 4xy() // 1
Subscript exposes parse
to build AST and compile
to create evaluators.
1import { parse, compile } from 'subscript' 2 3// parse expression 4let tree = parse('a.b + c - 1') 5tree // ['-', ['+', ['.', 'a', 'b'], 'c'], [,1]] 6 7// compile tree to evaluable function 8fn = compile(tree) 9fn({ a: {b: 1}, c: 2 }) // 2
AST has simplified lispy tree structure (inspired by frisk / nisp), opposed to ESTree:
1import { compile } from 'subscript.js' 2 3const fn = compile(['+', ['*', 'min', [,60]], [,'sec']]) 4fn({min: 5}) // min*60 + "sec" == "300sec" 5 6// node kinds 7['+', a]; // unary operator `+a` 8['+', a, b]; // binary operator `a + b` 9['+', a, b, c]; // n-ary operator `a + b + c` 10['()', a]; // group operator `(a)` 11['()', a, b]; // access operator `a(b)` 12[, 'a']; // literal value `'a'` 13a; // variable (from scope) 14null|empty; // placeholder 15 16// eg. 17['()', 'a'] // (a) 18['()', 'a', null] // a() 19['()', 'a', 'b'] // a(b) 20['++', 'a'] // ++a 21['++','a', null] // a++
To convert tree back to code, there's codegenerator function:
1import { stringify } from 'subscript.js' 2 3stringify(['+', ['*', 'min', [,60]], [,'sec']]) 4// 'min*60 + "sec" == "300sec"'
Subscript provides premade language features and API to customize syntax:
unary(str, precedence, postfix=false)
− register unary operator, either prefix ⚬a
or postfix a⚬
.binary(str, precedence, rassoc=false)
− register binary operator a ⚬ b
, optionally right-associative.nary(str, precedence)
− register n-ary (sequence) operator like a; b;
or a, b
, allows missing args.group(str, precedence)
- register group, like [a]
, {a}
, (a)
etc.access(str, precedence)
- register access operator, like a[b]
, a(b)
etc.token(str, precedence, lnode => node)
− register custom token or literal. Callback takes left-side node and returns complete expression node.operator(str, (a, b) => ctx => value)
− register evaluator for an operator. Callback takes node arguments and returns evaluator function.Longer operators should be registered after shorter ones, eg. first |
, then ||
, then ||=
.
1import script, { compile, operator, unary, binary, token } from './subscript.js' 2 3// enable objects/arrays syntax 4import 'subscript/feature/array.js'; 5import 'subscript/feature/object.js'; 6 7// add identity operators (precedence of comparison) 8binary('===', 9), binary('!==', 9) 9operator('===', (a, b) => (a = compile(a), b = compile(b), ctx => a(ctx)===b(ctx))) 10operator('===', (a, b) => (a = compile(a), b = compile(b), ctx => a(ctx)!==b(ctx))) 11 12// add nullish coalescing (precedence of logical or) 13binary('??', 3) 14operator('??', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) ?? b(ctx))) 15 16// add JS literals 17token('undefined', 20, a => a ? err() : [, undefined]) 18token('NaN', 20, a => a ? err() : [, NaN])
See ./feature/*
or ./justin.js
for examples.
Subscript shows good performance within other evaluators. Example expression:
1 + (a * b / c % d) - 2.0 + -3e-3 * +4.4e4 / f.g[0] - i.j(+k == 1)(0)
Parse 30k times:
subscript: ~150 ms 🥇
justin: ~183 ms
jsep: ~270 ms 🥈
jexpr: ~297 ms 🥉
mr-parser: ~420 ms
expr-eval: ~480 ms
math-parser: ~570 ms
math-expression-evaluator: ~900ms
jexl: ~1056 ms
mathjs: ~1200 ms
new Function: ~1154 ms
Eval 30k times:
new Function: ~7 ms 🥇
subscript: ~15 ms 🥈
justin: ~17 ms
jexpr: ~23 ms 🥉
jsep (expression-eval): ~30 ms
math-expression-evaluator: ~50ms
expr-eval: ~72 ms
jexl: ~110 ms
mathjs: ~119 ms
mr-parser: -
math-parser: -
jexpr, jsep, jexl, mozjexl, expr-eval, expression-eval, string-math, nerdamer, math-codegen, math-parser, math.js, nx-compile, built-in-math-eval
No vulnerabilities found.
No security vulnerabilities found.