buffalo-bench
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -103,5 +103,5 @@ // A benchmarking library that supports async hooks and benchmarks by default. | ||
import { version } from "../package.json"; | ||
import { version } from '../package.json'; | ||
let now = typeof performance === "undefined" ? () => Date.now() : () => performance.now(); | ||
let now = typeof performance === 'undefined' ? () => Date.now() : () => performance.now(); | ||
@@ -118,3 +118,3 @@ //*** Errors ***// | ||
constructor(message = "Something went wrong", code?: string) { | ||
constructor(message = 'Something went wrong', code?: string) { | ||
super(); | ||
@@ -130,3 +130,3 @@ this.message = message; | ||
statusCode = 1; | ||
name = "BeforeEachError"; | ||
name = 'BeforeEachError'; | ||
} | ||
@@ -137,3 +137,3 @@ | ||
statusCode = 2; | ||
name = "AfterEachError"; | ||
name = 'AfterEachError'; | ||
} | ||
@@ -144,3 +144,3 @@ | ||
statusCode = 3; | ||
name = "RunError"; | ||
name = 'RunError'; | ||
} | ||
@@ -151,3 +151,3 @@ | ||
statusCode = 4; | ||
name = "AfterError"; | ||
name = 'AfterError'; | ||
} | ||
@@ -158,3 +158,3 @@ | ||
statusCode = 5; | ||
name = "BeforeError"; | ||
name = 'BeforeError'; | ||
} | ||
@@ -165,3 +165,3 @@ | ||
statusCode = 7; | ||
name = "FatalError"; | ||
name = 'FatalError'; | ||
} | ||
@@ -179,3 +179,3 @@ | ||
type ErrorType = "BeforeEachError" | "AfterEachError" | "RunError" | "AfterError" | "BeforeError" | "FatalError"; | ||
type ErrorType = 'BeforeEachError' | 'AfterEachError' | 'RunError' | 'AfterError' | 'BeforeError' | 'FatalError'; | ||
@@ -221,11 +221,11 @@ // BenchmarkFunction a function that can be used as a benchmark. | ||
export const enum CompareBy { | ||
MeanTime = "meanTime", | ||
MedianTime = "medianTime", | ||
StandardDeviation = "standardDeviation", | ||
MaxTime = "maxTime", | ||
MinTime = "minTime", | ||
Hz = "hz", | ||
RunTime = "runTime", | ||
Cycles = "cycles", | ||
Percent = "percent" | ||
MeanTime = 'meanTime', | ||
MedianTime = 'medianTime', | ||
StandardDeviation = 'standardDeviation', | ||
MaxTime = 'maxTime', | ||
MinTime = 'minTime', | ||
Hz = 'hz', | ||
RunTime = 'runTime', | ||
Cycles = 'cycles', | ||
Percent = 'percent' | ||
} | ||
@@ -281,3 +281,3 @@ | ||
function isAsync(fn: BenchmarkFunction): boolean { | ||
return fn.constructor.name === "AsyncFunction"; | ||
return fn.constructor.name === 'AsyncFunction'; | ||
} | ||
@@ -295,3 +295,7 @@ | ||
} catch (error) { | ||
return getError(error, `Benchmark \`${instance.name}\` failed to run \`${callback.name}\` callback: ${error.message}`, errorTypeIfAny); | ||
return getError( | ||
error as Error, | ||
`Benchmark \`${instance.name}\` failed to run \`${callback.name}\` callback: ${(error as Error).message}`, | ||
errorTypeIfAny | ||
); | ||
} | ||
@@ -336,3 +340,3 @@ } | ||
if (typeof optionsOrFn === "function") { | ||
if (typeof optionsOrFn === 'function') { | ||
opts.fn = optionsOrFn; | ||
@@ -380,19 +384,19 @@ } else { | ||
switch (compareBy) { | ||
case "meanTime": | ||
case 'meanTime': | ||
return other.meanTime - meanTime; | ||
case "medianTime": | ||
case 'medianTime': | ||
return other.medianTime - medianTime; | ||
case "standardDeviation": | ||
case 'standardDeviation': | ||
return standardDeviation - other.standardDeviation; | ||
case "maxTime": | ||
case 'maxTime': | ||
return maxTime - other.maxTime; | ||
case "minTime": | ||
case 'minTime': | ||
return other.minTime - minTime; | ||
case "hz": | ||
case 'hz': | ||
return hz - other.hz; | ||
case "runTime": | ||
case 'runTime': | ||
return runTime - other.runTime; | ||
case "cycles": | ||
case 'cycles': | ||
return cycles - other.cycles; | ||
case "percent": | ||
case 'percent': | ||
return Math.trunc(((100 / meanTime) * other.meanTime - 100) * 100) / 100; | ||
@@ -412,3 +416,3 @@ default: | ||
this.cycles++; | ||
const BeforeEachError = await runCallback(this, "BeforeEachError", beforeEach); | ||
const BeforeEachError = await runCallback(this, 'BeforeEachError', beforeEach); | ||
if (BeforeEachError) { | ||
@@ -430,3 +434,3 @@ throw BeforeEachError; | ||
} catch (error) { | ||
throw getError(error, `Benchmark \`${this.name}\` failed to run \`fn\`: ${error.message}`, "RunError"); | ||
throw getError(error as Error, `Benchmark \`${this.name}\` failed to run \`fn\`: ${(error as Error).message}`, 'RunError'); | ||
} | ||
@@ -437,3 +441,3 @@ | ||
const AfterEachError = await runCallback(this, "AfterEachError", afterEach); | ||
const AfterEachError = await runCallback(this, 'AfterEachError', afterEach); | ||
if (AfterEachError) { | ||
@@ -454,3 +458,3 @@ throw AfterEachError; | ||
try { | ||
const beforeError = await runCallback(this, "BeforeError", before); | ||
const beforeError = await runCallback(this, 'BeforeError', before); | ||
if (beforeError) { | ||
@@ -477,3 +481,3 @@ throw beforeError; | ||
const afterError = await runCallback(this, "AfterError", after); | ||
const afterError = await runCallback(this, 'AfterError', after); | ||
if (afterError) { | ||
@@ -483,5 +487,5 @@ throw afterError; | ||
} catch (error) { | ||
this.error = error; | ||
this.error = error as BenchmarkError; | ||
const onErrorError = await runCallback(this, "FatalError", onError, error); | ||
const onErrorError = await runCallback(this, 'FatalError', onError, error); | ||
if (onErrorError) { | ||
@@ -596,3 +600,3 @@ throw onErrorError; | ||
if (typeof optionsOrFn === "function") { | ||
if (typeof optionsOrFn === 'function') { | ||
opts.fn = optionsOrFn; | ||
@@ -615,3 +619,3 @@ } else { | ||
try { | ||
const beforeError = await runCallback(this, "BeforeError", before); | ||
const beforeError = await runCallback(this, 'BeforeError', before); | ||
if (beforeError) { | ||
@@ -623,3 +627,3 @@ throw beforeError; | ||
let benchmark = this.benchmarks[i]; | ||
const beforeEachError = await runCallback(this, "BeforeEachError", beforeEach, benchmark, i); | ||
const beforeEachError = await runCallback(this, 'BeforeEachError', beforeEach, benchmark, i); | ||
if (beforeEachError) { | ||
@@ -633,3 +637,3 @@ throw beforeEachError; | ||
const afterEachError = await runCallback(this, "AfterEachError", afterEach, benchmark, i); | ||
const afterEachError = await runCallback(this, 'AfterEachError', afterEach, benchmark, i); | ||
if (afterEachError) { | ||
@@ -640,3 +644,3 @@ throw afterEachError; | ||
const afterError = await runCallback(this, "AfterError", after); | ||
const afterError = await runCallback(this, 'AfterError', after); | ||
if (afterError) { | ||
@@ -646,5 +650,5 @@ throw afterError; | ||
} catch (error) { | ||
this.error = error; | ||
this.error = error as BenchmarkError; | ||
const onErrorError = await runCallback(this, "FatalError", onError, error); | ||
const onErrorError = await runCallback(this, 'FatalError', onError, error); | ||
if (onErrorError) { | ||
@@ -651,0 +655,0 @@ throw onErrorError; |
{ | ||
"name": "buffalo-bench", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A benchmarking library that supports async hooks and benchmarks by default.", | ||
@@ -42,16 +42,16 @@ "source": "lib/index.ts", | ||
"dependencies": { | ||
"esbuild": "^0.12.19", | ||
"esbuild": "^0.13.13", | ||
"pirates": "^4.0.1" | ||
}, | ||
"devDependencies": { | ||
"@release-it/conventional-changelog": "^3.0.1", | ||
"@types/node": "^16.4.13", | ||
"@release-it/conventional-changelog": "^3.3.0", | ||
"@types/node": "^16.11.7", | ||
"cz-conventional-changelog": "^3.3.0", | ||
"nodemon": "^2.0.12", | ||
"release-it": "^14.10.1", | ||
"nodemon": "^2.0.15", | ||
"release-it": "^14.11.6", | ||
"remark-cli": "^10.0.0", | ||
"terser": "^5.7.1", | ||
"terser": "^5.9.0", | ||
"tsc": "^2.0.3", | ||
"tsc-prog": "^2.2.1", | ||
"typescript": "^4.3.5" | ||
"typescript": "^4.4.4" | ||
}, | ||
@@ -58,0 +58,0 @@ "config": { |
@@ -93,2 +93,38 @@ <div style="text-align: center"> | ||
### Suite example: | ||
```js | ||
let suite = new Benchmark.Suite("String comparison", { | ||
beforeEach(benchmark) { | ||
console.log(`${this.name}: ${benchmark.name}: Start`); | ||
}, | ||
afterEach(benchmark) { | ||
console.log(`${this.name}: ${benchmark.name}: End`); | ||
} | ||
}); | ||
suite.add("Direct comparison", () => "Hello World!" === "Hello World!"); | ||
suite.add("Regexp comparison", () => new RegExp("Hello World!").test("Hello World!")); | ||
suite.add("IndexOf comparison", () => "Hello World!".indexOf("Hello World!")); | ||
suite.add("Complex comparison", () => { | ||
let str = "Hello World!"; | ||
let str2 = "Hello World!"; | ||
let l = str.length; | ||
str.length === str2.length && str[0] === str2[0] && str[l - 1] === str2[l - 1] && str === str2; | ||
}); | ||
await suite.run(); | ||
// String comparison: Direct comparison: Start | ||
// String comparison: Direct comparison: End | ||
// String comparison: Regexp comparison: Start | ||
// String comparison: Regexp comparison: End | ||
// String comparison: IndexOf comparison: Start | ||
// String comparison: IndexOf comparison: End | ||
// String comparison: Complex comparison: Start | ||
// String comparison: Complex comparison: End | ||
let result = suite.compareFastestWithSlowest('percent'); | ||
console.log(result.fastest.name + " is faster than " + result.slowest.name + " by " + result.by + "%"); | ||
// Direct comparison is faster than Regexp comparison by 281.47% | ||
``` | ||
## Installation | ||
@@ -123,2 +159,3 @@ You can get this library as a [Node.js](https://nodejs.org/en/) module available through the [npm registry](https://www.npmjs.com/): | ||
* `cycles`: The number of cycles performed. | ||
* `samples`: The number of samples taken. | ||
* `hz`: The number of cycles per second. | ||
@@ -137,5 +174,5 @@ * `meanTime`: The meanTime time per cycle. | ||
The `Benchmark` instance has the following methods: | ||
* `run`: Async method that runs the benchmark. | ||
* `toJSON`: Return a JSON representation of the benchmark. | ||
* `compare`: Compare this benchmark to another. | ||
* `run()`: Async method that runs the benchmark. | ||
* `toJSON()`: Return a JSON representation of the benchmark. | ||
* `compareWith(other: Benchmark, compareBy: CompareBy)`: Compare this benchmark to the other benchmark and return a number representing the difference of the `CompareBy` metric between the two benchmarks. | ||
@@ -145,3 +182,42 @@ The `Benchmark` class has the following static properties: | ||
* `defaults`: An object containing the default options. | ||
* `Suite`: A class that represents a suite of benchmarks. | ||
The `Suite` constructor takes a `name` and an `options` object argument with the following properties: | ||
* `maxTime`: The maximum time in seconds that a benchmark can take including hooks. | ||
* `minSamples`: The minimum number of samples that must be taken. | ||
* `beforeEach`: A function to be run once before each benchmark run, does not count for run time. | ||
* `afterEach`: A function to be run once after each benchmark run, does not count for run time. | ||
* `before`: A function to be run once before the benchmark run starts, does not count for run time. | ||
* `after`: A function to be run once after the benchmark run finishes, does not count for run time. | ||
* `onError`: A function to be run if an error occurs. | ||
The `Suite` instance has the following properties: | ||
* `name`: The name of the suite. | ||
* `error`: The error object if an error occurred. | ||
* `options`: The options object passed to the constructor. | ||
* `stamp`: A timestamp representing when the suite was created. | ||
* `runTime`: The total time taken to run the suite, this does not include beforeEach, afterEach, before and after hooks. | ||
* `totalTime`: The total time taken to run the suite including beforeEach, afterEach, before and after hooks. | ||
* `benchmarks`: An array of the benchmarks in the suite. | ||
The `Suite` instance has the following methods: | ||
* `add(name: string, optionsOrFn: BenchmarkOptions | Function, options?: BenchmarkOptions)`: Add a benchmark to the suite. | ||
* `getSortedBenchmarksBy(sortedBy: CompareBy)`: Get the benchmarks sorted by a given `CompareBy` metric. | ||
* `getFastest(sortedBy: CompareBy)`: Get the fastest benchmark in the suite sorting by the given `CompareBy` metric. | ||
* `getSlowest(sortedBy: CompareBy)`: Get the slowest benchmark in the suite sorting by the given `CompareBy` metric. | ||
* `CompareFastestWithSlowest(compareBy: CompareBy)`: Compare the fastest benchmark with the slowest benchmark sorting by the given `CompareBy` metric. | ||
* `run`: Async method that runs the suite. | ||
* `toJSON`: Return a JSON representation of the suite. | ||
The `CompareBy` enum has the following values: | ||
* `meanTime`: Compare by the mean time per cycle. | ||
* `medianTime`: Compare by the median time per cycle. | ||
* `standardDeviation`: Compare by the standard deviation. | ||
* `maxTime`: Compare by the maximum time. | ||
* `minTime`: Compare by the minimum time. | ||
* `hz`: Compare by the number of cycles per second. | ||
* `runTime`: Compare by the total time taken to run the suite. | ||
* `cycles`: Compare by the number of cycles. | ||
* `percent`: Compare by the percentage of cycles that were slower than the fastest benchmark. | ||
### Api Notes | ||
@@ -148,0 +224,0 @@ |
@@ -1,23 +0,23 @@ | ||
const { addHook } = require("pirates"); | ||
const { transformSync } = require("esbuild"); | ||
const fs = require("fs"); | ||
const { addHook } = require('pirates'); | ||
const { transformSync } = require('esbuild'); | ||
const fs = require('fs'); | ||
addHook( | ||
(code, filePath) => { | ||
let fileName = filePath.split("/").pop(); | ||
let extension = fileName.split(".").pop(); | ||
let fileName = filePath.split('/').pop(); | ||
let extension = fileName.split('.').pop(); | ||
let loader = "default"; | ||
if (["js", "jsx", "ts", "tsx", "css", "json", "txt"].includes(extension)) { | ||
if (["js", "jsx", "mjs"].includes(extension)) { | ||
loader = "jsx"; | ||
} else if (["ts", "tsx"].includes(extension)) { | ||
loader = "tsx"; | ||
} else if (extension === "txt") { | ||
loader = "text"; | ||
let loader = 'default'; | ||
if (['js', 'jsx', 'ts', 'tsx', 'css', 'json', 'txt'].includes(extension)) { | ||
if (['js', 'jsx', 'mjs'].includes(extension)) { | ||
loader = 'jsx'; | ||
} else if (['ts', 'tsx'].includes(extension)) { | ||
loader = 'tsx'; | ||
} else if (extension === 'txt') { | ||
loader = 'text'; | ||
} else { | ||
loader = extension; | ||
} | ||
} else if (["jpeg", "jpg", "png", "gif", "webp", "svg"].includes(extension)) { | ||
loader = "dataurl"; | ||
} else if (['jpeg', 'jpg', 'png', 'gif', 'webp', 'svg'].includes(extension)) { | ||
loader = 'dataurl'; | ||
} | ||
@@ -28,4 +28,4 @@ | ||
compilerOptions: { | ||
target: "ESNEXT", | ||
module: "ESNEXT", | ||
target: 'ESNEXT', | ||
module: 'ESNEXT', | ||
strict: true, | ||
@@ -40,10 +40,10 @@ allowSyntheticDefaultImports: true, | ||
minify: false, | ||
format: "cjs", | ||
target: "esnext", | ||
logLevel: "warning" | ||
format: 'cjs', | ||
target: 'esnext', | ||
logLevel: 'warning' | ||
}; | ||
// Check if tsconfig.json exists with fs module | ||
if ((extension === "ts" || extension === "tsx") && fs.existsSync(process.cwd() + "/tsconfig.json")) { | ||
let tsconfig = fs.readFileSync(process.cwd() + "/tsconfig.json", "utf8"); | ||
if ((extension === 'ts' || extension === 'tsx') && fs.existsSync(process.cwd() + '/tsconfig.json')) { | ||
let tsconfig = fs.readFileSync(process.cwd() + '/tsconfig.json', 'utf8'); | ||
@@ -62,6 +62,6 @@ let tsconfigRaw = JSON.parse(tsconfig); | ||
let format = compilerOptions.module.toLowerCase(); | ||
if (format === "commonjs") { | ||
options.format = "cjs"; | ||
} else if (format.startsWith("es")) { | ||
options.format = "esm"; | ||
if (format === 'commonjs') { | ||
options.format = 'cjs'; | ||
} else if (format.startsWith('es')) { | ||
options.format = 'esm'; | ||
} | ||
@@ -73,3 +73,3 @@ } | ||
if (/"use strict"\;/gi.test(code) === false) { | ||
transformed = '"use strict";\n' + transformed; | ||
transformed = '"use strict";' + transformed; | ||
} | ||
@@ -80,3 +80,3 @@ | ||
{ | ||
exts: [".js", ".jsx", ".ts", ".tsx", ".mjs", ".css", ".json", ".text", ".jpeg", ".jpg", ".png", ".gif", ".webp", ".svg", ".html"], | ||
exts: ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.css', '.json', '.text', '.jpeg', '.jpg', '.png', '.gif', '.webp', '.svg', '.html'], | ||
ignoreNodeModules: false, | ||
@@ -83,0 +83,0 @@ matcher(fileName) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
92665
654
278
7
+ Addedesbuild@0.13.15(transitive)
+ Addedesbuild-android-arm64@0.13.15(transitive)
+ Addedesbuild-darwin-64@0.13.15(transitive)
+ Addedesbuild-darwin-arm64@0.13.15(transitive)
+ Addedesbuild-freebsd-64@0.13.15(transitive)
+ Addedesbuild-freebsd-arm64@0.13.15(transitive)
+ Addedesbuild-linux-32@0.13.15(transitive)
+ Addedesbuild-linux-64@0.13.15(transitive)
+ Addedesbuild-linux-arm@0.13.15(transitive)
+ Addedesbuild-linux-arm64@0.13.15(transitive)
+ Addedesbuild-linux-mips64le@0.13.15(transitive)
+ Addedesbuild-linux-ppc64le@0.13.15(transitive)
+ Addedesbuild-netbsd-64@0.13.15(transitive)
+ Addedesbuild-openbsd-64@0.13.15(transitive)
+ Addedesbuild-sunos-64@0.13.15(transitive)
+ Addedesbuild-windows-32@0.13.15(transitive)
+ Addedesbuild-windows-64@0.13.15(transitive)
+ Addedesbuild-windows-arm64@0.13.15(transitive)
- Removedesbuild@0.12.29(transitive)
Updatedesbuild@^0.13.13