mathjs/test/benchmark/unit_parser.js
2024-05-22 08:46:14 +02:00

41 lines
1.0 KiB
JavaScript

// test performance of the unit expression parser in node.js
// browserify benchmark/unit_parser.js -o ./benchmark_unit_parser.js
import Benchmark from 'benchmark'
import { Unit, evaluate } from '../../lib/esm/index.js'
// expose on window when using bundled in a browser
if (typeof window !== 'undefined') {
window.Benchmark = Benchmark
}
const expr = '[1mm, 2mm, 3mm, 4mm, 5mm, 6mm, 7mm, 8mm, 9mm, 10mm]'
console.log('Unit.parse expression: mm')
console.log('evaluate expression:', expr)
let total = 0
const suite = new Benchmark.Suite()
suite
.add('Unit.parse', function () {
total += Unit.parse('mm').dimensions[0]
})
.add('evaluate', function () {
total += evaluate(expr).size()[0]
})
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
// we count at total to prevent the browsers from not executing
// the benchmarks ("dead code") when the results would not be used.
if (total > 5) {
console.log('')
} else {
console.log('')
}
})
.run()