Skip to content

Commit

Permalink
lib: replace cjs to esm
Browse files Browse the repository at this point in the history
sad days. But, it will make the design easier
to replace benchmarkjs to tinybench as the API
expects to have a top-level-await
  • Loading branch information
RafaelGSS committed May 8, 2024
1 parent f36f092 commit 81f97b1
Show file tree
Hide file tree
Showing 41 changed files with 77 additions and 89 deletions.
2 changes: 1 addition & 1 deletion bench/add-property.js → bench/add-property.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Adding property')

Expand Down
2 changes: 1 addition & 1 deletion bench/array-append.js → bench/array-append.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { H2, createTableHeader } = require('../markdown')
import { H2, createTableHeader } from '../markdown.js'

function compareToMdTable(name, amount, ms) {
const numberFormat = new Intl.NumberFormat()
Expand Down
2 changes: 1 addition & 1 deletion bench/array-creation.js → bench/array-creation.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Array Creation')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs';

const suite = createBenchmarkSuite('async function vs function');

Expand Down
10 changes: 5 additions & 5 deletions bench/blob.js → bench/blob.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const { skipIfVersionWithMessage } = require('../utils')
skipIfVersionWithMessage('19.8.1||19.8.0', 'Blob')
import { skipIfVersionWithMessage } from '../utils'
import { createBenchmarkSuite } from '../common.mjs'
import { Blob } from 'buffer'

const { createBenchmarkSuite } = require('../common')
skipIfVersionWithMessage('19.8.1||19.8.0', 'Blob')
skipIfVersionWithMessage('16.0.0', 'Blob')

const suite = createBenchmarkSuite('Blob')

const { Blob } = require('buffer')

const source128 = Buffer.allocUnsafe(128)
const source1024 = Buffer.allocUnsafe(1024)
const blob128 = new Blob(source128)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Comparison using `instanceof`')

Expand Down
11 changes: 5 additions & 6 deletions bench/crypto-verify.js → bench/crypto-verify.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'
import { createSign, createVerify, verify } from 'crypto'

const suite = createBenchmarkSuite('Crypto Verify')

const crypto = require('crypto')

const rsaPrivateKey = `
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAz7QweYwr7puNtc8/6jf4pADXfY2/HVq7LgI7JXkAwW5HTEjU
Expand Down Expand Up @@ -48,15 +47,15 @@ uQIDAQAB

const thing = 'hello world'
const algorithm = 'RSA-SHA256'
const signature = crypto.createSign(algorithm).update(thing).sign(rsaPrivateKey, 'base64')
const signature = createSign(algorithm).update(thing).sign(rsaPrivateKey, 'base64')

suite
.add(`crypto.createVerify('${algorithm}')`, function () {
var verifier = crypto.createVerify(algorithm)
var verifier = createVerify(algorithm)
verifier.update(thing)
verifier.verify(rsaPublicKey, signature, 'base64')
})
.add(`crypto.verify('${algorithm}')`, function () {
crypto.verify(algorithm, thing, rsaPublicKey, Buffer.from(signature, 'base64'))
verify(algorithm, thing, rsaPublicKey, Buffer.from(signature, 'base64'))
})
.run({ async: false })
5 changes: 2 additions & 3 deletions bench/date-format-iso.js → bench/date-format-iso.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'
import { fromUnixToISOString } from '../utils/from-unix-to-iso-string'

const suite = createBenchmarkSuite('Date toISOString')

const { fromUnixToISOString } = require('../utils/from-unix-to-iso-string')

suite
.add('new Date().toISOString()', function () {
new Date().toISOString()
Expand Down
2 changes: 1 addition & 1 deletion bench/date-format.js → bench/date-format.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Date format MM/DD/YYYY')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Date String coersion')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Deleting properties')

Expand Down
2 changes: 1 addition & 1 deletion bench/error.js → bench/error.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Node.js Error')

Expand Down
2 changes: 1 addition & 1 deletion bench/function-return.js → bench/function-return.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Function return')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Array.includes vs raw comparison')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Object.keys vs Object.getOwnPropertyNames comparison')

Expand Down
2 changes: 1 addition & 1 deletion bench/last-array-item.js → bench/last-array-item.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Get the last item of an Array')

Expand Down
2 changes: 1 addition & 1 deletion bench/object-creation.js → bench/object-creation.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Object Creation')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Optional Chain (?) vs && operator')

Expand Down
2 changes: 1 addition & 1 deletion bench/parse-int.js → bench/parse-int.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Parsing Integer')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Possible undefined Function')

Expand Down
4 changes: 2 additions & 2 deletions bench/private-property.js → bench/private-property.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'
import PrivateSymbol from 'privsym'

const suite = createBenchmarkSuite('Private Property')

const kWidth = Symbol('width')
const kHeight = Symbol('height')
const PrivateSymbol = require('privsym')
const pkWidth = PrivateSymbol('width')
const pkHeight = PrivateSymbol('height')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Property access after shape transition')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Property Getter Access')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Property Setter Access')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { skipIfVersionWithMessage } = require('../utils')
skipIfVersionWithMessage('<15.0.0', 'replace vs replaceAll comparison')
import { skipIfVersionWithMessage } from '../utils'
import { createBenchmarkSuite } from '../common.mjs'

const { createBenchmarkSuite } = require('../common')
skipIfVersionWithMessage('<15.0.0', 'replace vs replaceAll comparison')

const suite = createBenchmarkSuite('replace vs replaceAll comparison')

Expand Down
2 changes: 1 addition & 1 deletion bench/shallow-copy.js → bench/shallow-copy.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Shallow Copy')

Expand Down
2 changes: 1 addition & 1 deletion bench/sort-map.js → bench/sort-map.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Sorting Map')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Object.assign VS spread operator')

Expand Down
9 changes: 5 additions & 4 deletions bench/stream-readable.js → bench/stream-readable.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { skipIfVersion } = require('../utils')
import { skipIfVersion } from '../utils'
import { Readable } from 'node:stream'
import { ReadableStream } from 'node:stream/web'
import { createBenchmarkSuite } from '../common.mjs'

skipIfVersion('<= 16.5.0')
const { Readable } = require('node:stream')
const { ReadableStream } = require('node:stream/web')
const { createBenchmarkSuite } = require('../common')

const suite = createBenchmarkSuite('Stream.Readable')

Expand Down
9 changes: 5 additions & 4 deletions bench/stream-writable.js → bench/stream-writable.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { skipIfVersion } = require('../utils')
import { skipIfVersion } from '../utils'
import { Writable } from 'node:stream'
import { WritableStream } from 'node:stream/web'
import { createBenchmarkSuite } from '../common.mjs'

skipIfVersion('<= 16.5.0')
const { Writable } = require('node:stream')
const { WritableStream } = require('node:stream/web')
const { createBenchmarkSuite } = require('../common')

const suite = createBenchmarkSuite('Stream.Writable')

Expand Down
2 changes: 1 addition & 1 deletion bench/string-concat.js → bench/string-concat.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require("../common")
import { createBenchmarkSuite } from "../common.mjs"

const suite = createBenchmarkSuite('String concat')

Expand Down
2 changes: 1 addition & 1 deletion bench/string-endsWith.js → bench/string-endsWith.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('endsWith comparison')
const shortString = 'foobar'
Expand Down
2 changes: 1 addition & 1 deletion bench/string-searching.js → bench/string-searching.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('String searching')

Expand Down
2 changes: 1 addition & 1 deletion bench/string-startsWith.js → bench/string-startsWith.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('startsWith comparison')
const shortString = 'foobar'
Expand Down
2 changes: 1 addition & 1 deletion bench/super-vs-this.js → bench/super-vs-this.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Super vs This')

Expand Down
2 changes: 1 addition & 1 deletion bench/unix-time.js → bench/unix-time.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createBenchmarkSuite } = require('../common')
import { createBenchmarkSuite } from '../common.mjs'

const suite = createBenchmarkSuite('Getting unix time')

Expand Down
File renamed without changes.
29 changes: 11 additions & 18 deletions common.js → common.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const Benchmark = require('benchmark')
const { createTableHeader, H2, eventToMdTable } = require('./markdown')
const os = require('os')
import Benchmark from 'benchmark'
import { createTableHeader, H2, eventToMdTable } from './markdown.mjs'
import { platform, arch, cpus, totalmem } from 'os'

function installMarkdownEmitter(suite, name, tableHeaderColumns = ['name', 'ops/sec', 'samples']) {
export function installMarkdownEmitter(suite, name, tableHeaderColumns = ['name', 'ops/sec', 'samples']) {
const tableHeader = createTableHeader(tableHeaderColumns)

suite
Expand All @@ -17,14 +17,14 @@ function installMarkdownEmitter(suite, name, tableHeaderColumns = ['name', 'ops/

function getMachineInfo() {
return {
platform: os.platform(),
arch: os.arch(),
cpus: os.cpus().length,
totalMemory: os.totalmem() / 1024 ** 3,
platform: platform(),
arch: arch(),
cpus: cpus().length,
totalMemory: totalmem() / 1024 ** 3,
}
}

function installMarkdownMachineInfo(suite) {
export function installMarkdownMachineInfo(suite) {
if (!process.env.CI) return

const { platform, arch, cpus, totalMemory } = getMachineInfo()
Expand All @@ -46,7 +46,7 @@ function installMarkdownMachineInfo(suite) {
})
}

function installMarkdownHiddenDetailedInfo(suite) {
export function installMarkdownHiddenDetailedInfo(suite) {
if (!process.env.CI) return

const cycleEvents = []
Expand All @@ -73,7 +73,7 @@ function installMarkdownHiddenDetailedInfo(suite) {
})
}

function createBenchmarkSuite(name, { tableHeaderColumns = ['name', 'ops/sec', 'samples'] } = {}) {
export function createBenchmarkSuite(name, { tableHeaderColumns = ['name', 'ops/sec', 'samples'] } = {}) {
const suite = new Benchmark.Suite()

installMarkdownEmitter(suite, name, tableHeaderColumns)
Expand All @@ -82,10 +82,3 @@ function createBenchmarkSuite(name, { tableHeaderColumns = ['name', 'ops/sec', '

return suite
}

module.exports = {
createBenchmarkSuite,
installMarkdownEmitter,
installMarkdownMachineInfo,
installMarkdownHiddenDetailedInfo,
}
12 changes: 3 additions & 9 deletions markdown.js → markdown.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function eventToMdTable (event) {
export function eventToMdTable (event) {
const { target } = event
return `|${target.name}|${Math.round(target.hz).toLocaleString()}|${target.stats.sample.length}|`
}

function createTableHeader(columns) {
export function createTableHeader(columns) {
let header = '|'
let headerSep = '|'
for (const col of columns) {
Expand All @@ -14,12 +14,6 @@ function createTableHeader(columns) {
return `${header}\n${headerSep}`
}

function H2(title) {
export function H2(title) {
return `## ${title}\n`
}

module.exports = {
eventToMdTable,
createTableHeader,
H2,
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"fastify": "^4.4.0",
"on-net-listen": "^1.1.2",
"privsym": "^1.0.0",
"semver": "^7.5.4"
"semver": "^7.5.4",
"tinybench": "^2.8.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
Loading

0 comments on commit 81f97b1

Please sign in to comment.