Skip to content

⚡️ Fast MagicString port driven by Rust and N-API

License

Notifications You must be signed in to change notification settings

h-a-n-a/magic-string-rs

Repository files navigation


magic-string-rs

CI crates NPM version

MagicString port for Node and modern browsers, also, for rust, of course.


🔧 Installation

Rust

Add it as a dependency in a Cargo project.

# Cargo.toml

[dependency]
magic_string = "x.x.x"

Node

$ npm install @napi-rs/magic-string

Note: Web-Assembly is currently not supported, but it's on the plan.

Performance

Hardware info

Model Name:	MacBook Pro
Model Identifier:	MacBookPro17,1
Chip:	Apple M1
Total Number of Cores:	8 (4 performance and 4 efficiency)
Memory:	16 GB

Benchmark

Running "overwrite" suite...
Progress: 100%

  MagicString:
    238 584 ops/s, ±0.34%   | slowest, 50.7% slower

  MagicStringRust:
    483 950 ops/s, ±2.13%   | fastest

Finished 2 cases!
  Fastest: MagicStringRust
  Slowest: MagicString

Running "prepend|append" suite...
Progress: 100%

  MagicString:
    290 244 ops/s, ±1.35%   | slowest, 48.35% slower

  MagicStringRust:
    561 981 ops/s, ±6.71%   | fastest

Finished 2 cases!
  Fastest: MagicStringRust
  Slowest: MagicString

Running "add banner#toString" suite...
Progress: 100%

  MagicString:
    301 467 ops/s, ±0.29%   | slowest, 37.66% slower

  MagicStringRust:
    483 586 ops/s, ±5.50%   | fastest

Finished 2 cases!
  Fastest: MagicStringRust
  Slowest: MagicString

Running "add banner#generateDecodedMap" suite...
Progress: 100%

  MagicString:
    233 702 ops/s, ±0.76%   | fastest

  MagicStringRust:
    229 899 ops/s, ±2.68%   | slowest, 1.63% slower

Finished 2 cases!
  Fastest: MagicString
  Slowest: MagicStringRust

Running "add banner#generateMapHires" suite...
Progress: 100%

  MagicString:
    177 783 ops/s, ±1.84%   | fastest

  MagicStringRust:
    90 780 ops/s, ±1.00%    | slowest, 48.94% slower

Finished 2 cases!
  Fastest: MagicString
  Slowest: MagicStringRust

Running "add banner#generateMap" suite...
Progress: 100%

  MagicString:
    227 594 ops/s, ±0.68%   | slowest, 0.42% slow
er

  MagicStringRust:
    228 545 ops/s, ±0.82%   | fastest

Finished 2 cases!
  Fastest: MagicStringRust
  Slowest: MagicString

Running "add banner#generateMap.toString" suite...
Progress: 100%

  MagicString:
    201 272 ops/s, ±0.47%   | slowest, 21.86% slower

  MagicStringRust:
    257 577 ops/s, ±2.38%   | fastest

Finished 2 cases!
  Fastest: MagicStringRust
  Slowest: MagicString

Running "add banner#generateMapHires.toString" suite...
Progress: 100%

  MagicString:
    157 685 ops/s, ±0.18%   | fastest

  MagicStringRust:
    95 510 ops/s, ±1.00%    | slowest, 39.43% slower

Finished 2 cases!
  Fastest: MagicString
  Slowest: MagicStringRust

Running "add banner#generateMap.toUrl" suite...
Progress: 100%

  MagicString:
    182 161 ops/s, ±0.65%   | slowest, 25.04% slower

  MagicStringRust:
    243 019 ops/s, ±0.98%   | fastest

Finished 2 cases!
  Fastest: MagicStringRust
  Slowest: MagicString

📃 Documentation

doc.rs

Supported APIs

  • generateMap: Note that there is a huge overhead for rust for implementing the same API in Node, for more detail please refer to this
  • generateDecodedMap
  • toString
  • prepend
  • append
  • prependLeft
  • prependRight
  • appendLeft
  • appendRight
  • overwrite
  • trim
  • trimStart
  • trimEnd
  • trimLines
  • isEmpty
  • remove
  • move
  • indent
  • addSourcemapLocation
  • clone
  • slice
  • snip

Credits

The original project magic-string is really awesome, you should check it out and we made this project even furthur for better performance.

License

MIT