Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nanobench

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nanobench - npm Package Compare versions

Comparing version 2.1.1 to 3.0.0

58

compare.js
#!/usr/bin/env node
var fs = require('fs')
var parse = require('./parse')
var prettyHrtime = require('pretty-hrtime')
var chalk = require('chalk')
const fs = require('fs')
const prettyHrtime = require('pretty-hrtime')
const chalk = require('chalk')
const parse = require('./parse')
var grace = 0.05 // 5% grace
var a = parse(fs.readFileSync(process.argv[2]))
var b = parse(fs.readFileSync(process.argv[3]))
const grace = 0.05 // 5% grace
const a = parse(fs.readFileSync(process.argv[2]))
const b = parse(fs.readFileSync(process.argv[3]))
var upper = 1 + grace
var lower = 1 - grace
const upper = 1 + grace
const lower = 1 - grace
var mapA = toMap(a)
var mapB = toMap(b)
var list = []
const mapA = toMap(a)
const mapB = toMap(b)
const list = []

@@ -28,11 +28,11 @@ a.benchmarks.forEach(function (bench) {

write('NANOBENCH version 2', '|', 'NANOBENCH version 2')
write('> ' + a.command, '|', '> ' + b.command)
write('', '|', '')
write('> ' + a.command, '|', '> ' + b.command)
write('', '|', '')
list.forEach(function (bench) {
var left = bench
var right = bench.other
let left = bench
let right = bench.other
if (mapB[left.name] === left) {
var tmp = left
const tmp = left
left = right

@@ -42,3 +42,3 @@ right = tmp

var sep = '='
let sep = '='
if (left.winner) sep = '<'

@@ -49,4 +49,4 @@ if (right && right.winner) sep = '>'

var len = Math.max(left.output.length, right ? right.output.length : 0)
for (var i = 0; i < len; i++) {
const len = Math.max(left.output.length, right ? right.output.length : 0)
for (let i = 0; i < len; i++) {
write(left.output[i] && '# ' + left.output[i], sep, right.output[i] && '# ' + right.output[i])

@@ -59,4 +59,4 @@ }

var last = compare(a.time, b.time)
var sep = last === 1 ? '>' : last === -1 ? '<' : '='
const last = compare(a.time, b.time)
const sep = last === 1 ? '>' : last === -1 ? '<' : '='

@@ -94,11 +94,11 @@ write('all benchmarks completed', sep, 'all benchmarks completed')

console.log(a + sep + ' ' +b)
console.log(a + sep + ' ' + b)
}
function compareBench (bench, map) {
var other = map[bench.name]
const other = map[bench.name]
if (!other) return
if (list.indexOf(other) > -1) return
var cmp = compare(bench.time, other.time)
const cmp = compare(bench.time, other.time)

@@ -108,7 +108,7 @@ bench.other = other

var winner = null
let winner = null
if (cmp === 1) winner = other
if (cmp === -1) winner = bench
var loser = winner === bench ? other : bench
const loser = winner === bench ? other : bench

@@ -122,3 +122,3 @@ if (winner) {

function compare (a, b) {
var pct = (a[0] * 1e9 + a[1]) / (b[0] * 1e9 + b[1])
const pct = (a[0] * 1e9 + a[1]) / (b[0] * 1e9 + b[1])
if (pct > upper) return 1

@@ -130,3 +130,3 @@ if (pct < lower) return -1

function toMap (output) {
var map = {}
const map = {}
output.benchmarks.forEach(function (b) {

@@ -133,0 +133,0 @@ map[b.name] = b

@@ -1,10 +0,10 @@

var bench = require('./')
const bench = require('./')
bench('sha1 200.000 times', function (b) {
var crypto = require('crypto')
var data = new Buffer('hello world')
const crypto = require('crypto')
let data = Buffer.from('hello world')
b.start()
for (var i = 0; i < 200000; i++) {
for (let i = 0; i < 200000; i++) {
data = crypto.createHash('sha1').update(data).digest()

@@ -17,8 +17,8 @@ }

bench('sha256 200.000 times', function (b) {
var crypto = require('crypto')
var data = new Buffer('hello world')
const crypto = require('crypto')
let data = Buffer.from('hello world')
b.start()
for (var i = 0; i < 200000; i++) {
for (let i = 0; i < 200000; i++) {
data = crypto.createHash('sha256').update(data).digest()

@@ -25,0 +25,0 @@ }

@@ -1,10 +0,9 @@

var mutexify = require('mutexify')
var hrtime = require('browser-process-hrtime')
var prettyHrtime = require('pretty-hrtime')
var path = require('path')
var lock = mutexify()
var one = false
var cur = null
var runs = 0
var total = [0, 0]
const mutexify = require('mutexify')
const prettyHrtime = require('pretty-hrtime')
const path = require('path')
const lock = mutexify()
let one = false
let cur = null
let runs = 0
const total = [0, 0]

@@ -25,2 +24,6 @@ module.exports = global.__NANOBENCH__ ? require(global.__NANOBENCH__) : benchmark

function toMillis (hr) {
return hr[0] * 1e3 + hr[1] / 1e6
}
function benchmark (name, fn, only) {

@@ -37,7 +40,7 @@ process.nextTick(function () {

var b = cur = {}
var begin = hrtime()
const b = cur = {}
let begin = process.hrtime()
b.start = function () {
begin = hrtime()
begin = process.hrtime()
}

@@ -55,2 +58,6 @@

b.elapsed = function () {
return toMillis(process.hrtime(begin))
}
b.end = function (msg) {

@@ -60,6 +67,6 @@ if (msg) b.log(msg)

cur = null
var time = hrtime(begin)
const elapsed = process.hrtime(begin)
total[0] += time[0]
total[1] += time[1]
total[0] += elapsed[0]
total[1] += elapsed[1]
while (total[1] >= 1e9) {

@@ -70,4 +77,5 @@ total[1] -= 1e9

console.log('ok ~' + prettyHrtime(time) + ' ' + rawTime(time) + '\n')
console.log('ok ~' + prettyHrtime(elapsed) + ' ' + rawTime(elapsed) + '\n')
release()
return toMillis(elapsed)
}

@@ -91,3 +99,3 @@

function command () {
var argv = process.argv.slice(0)
let argv = process.argv.slice(0)
if (argv[0] === '/usr/local/bin/node') argv[0] = 'node'

@@ -100,3 +108,3 @@ if (argv[1] === path.join(__dirname, 'run.js')) {

argv = argv.map(function (name) {
var cwd = process.cwd() + path.sep
const cwd = process.cwd() + path.sep
return name.indexOf(cwd) === 0 ? name.slice(cwd.length) : name

@@ -103,0 +111,0 @@ })

{
"name": "nanobench",
"version": "2.1.1",
"version": "3.0.0",
"description": "Simple benchmarking tool with TAP-like output that is easy to parse",
"main": "index.js",
"dependencies": {
"browser-process-hrtime": "^0.1.2",
"chalk": "^1.1.3",
"mutexify": "^1.1.0",
"chalk": "^5.0.1",
"mutexify": "^1.4.0",
"pretty-hrtime": "^1.0.2"
},
"devDependencies": {
"standard": "^8.5.0"
"standard": "^17.0.0"
},

@@ -15,0 +14,0 @@ "scripts": {

@@ -1,5 +0,3 @@

module.exports = parse
function parse (data) {
var lines = data.toString().trim().split('\n')
module.exports = function parse (data) {
const lines = data.toString().trim().split('\n')
.map(function (line) {

@@ -12,6 +10,6 @@ return line.trim()

var output = {}
const output = {}
while (lines.length && !output.type) {
var header = lines.shift().match(/^NANOBENCH(?: version (\d+))?$/)
const header = lines.shift().match(/^NANOBENCH(?: version (\d+))?$/)
if (!header) continue

@@ -34,6 +32,6 @@ output.type = 'NANOBENCH'

var benchmark = null
let benchmark = null
while (lines.length) {
var next = lines.shift()
const next = lines.shift()
if (next[0] === '>') {

@@ -45,3 +43,3 @@ output.command = next.slice(1).trim()

if (!benchmark && next[0] === '#') {
benchmark = {name: null, output: [], error: null, time: null}
benchmark = { name: null, output: [], error: null, time: null }
benchmark.name = next.slice(1).trim()

@@ -86,8 +84,8 @@ continue

function time (line) {
var i = line.lastIndexOf('(')
var j = line.lastIndexOf(')')
const i = line.lastIndexOf('(')
const j = line.lastIndexOf(')')
if (i === -1 || j === -1 || j < i) throw new Error('Could not parse benchmark time')
var parsed = line.slice(i + 1, j).match(/^(\d+)\s*s\s*\+\s*(\d+)\s*ns$/)
const parsed = line.slice(i + 1, j).match(/^(\d+)\s*s\s*\+\s*(\d+)\s*ns$/)
if (!parsed) throw new Error('Could not parse benchmark time')
return [Number(parsed[1]), Number(parsed[2])]
}

@@ -64,3 +64,4 @@ # nanobench

* `b.start()` - Start the benchmark. If not called the bench will be tracked from the beginning of the function.
* `b.end()` - End the benchmark.
* `b.end()` - End the benchmark. Returns the time elapsed in milliseconds.
* `b.elapsed()` - Return the time elapsed in milliseconds.
* `b.error(err)` - Benchmark failed. Report error.

@@ -67,0 +68,0 @@ * `b.log(msg)` - Log out a message

#!/usr/bin/env node
var bench = require('./')
var path = require('path')
const path = require('path')
global.__NANOBENCH__ = require.resolve('./')
for (var i = 2; i < process.argv.length; i++) require(path.join(process.cwd(), process.argv[i]))
for (let i = 2; i < process.argv.length; i++) require(path.join(process.cwd(), process.argv[i]))
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc