-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
1,958 additions
and
2,037 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"synckit": minor | ||
--- | ||
|
||
feat: use native esm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SYNCKIT_TS_ESM=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// @ts-check | ||
|
||
const { performance } = require('perf_hooks') | ||
|
||
const RUN_TIMES = +process.env.RUN_TIMES || 1000 | ||
|
||
/** | ||
* @param {string} name | ||
* @typedef {{ loadTime:number,runTime:number, totalTime:number }} PerfResult | ||
* @returns {PerfResult} Perf result | ||
*/ | ||
const perfCase = name => { | ||
const loadStartTime = performance.now() | ||
|
||
const syncFn = require(`./${name}.cjs`) | ||
|
||
const loadTime = performance.now() - loadStartTime | ||
|
||
let i = RUN_TIMES | ||
|
||
const runStartTime = performance.now() | ||
|
||
while (i-- > 0) { | ||
syncFn(__filename) | ||
} | ||
|
||
const runTime = performance.now() - runStartTime | ||
|
||
return { | ||
loadTime, | ||
runTime, | ||
totalTime: runTime + loadTime, | ||
} | ||
} | ||
|
||
/** | ||
* @param {string} text | ||
* @returns {string} Kebab cased text | ||
*/ | ||
const kebabCase = text => | ||
text.replace(/([A-Z]+)/, (_, $1) => '-' + $1.toLowerCase()) | ||
|
||
class Benchmark { | ||
/** | ||
* @param { Object.<string, PerfResult> } perfResults | ||
*/ | ||
constructor(perfResults) { | ||
const keys = Object.keys(perfResults) | ||
const _baseKey = keys[0] | ||
const baseKey = kebabCase(_baseKey) | ||
|
||
const perfTypes = /** @type {Array<keyof PerfResult>} */ ([ | ||
'loadTime', | ||
'runTime', | ||
'totalTime', | ||
]) | ||
|
||
for (const perfType of perfTypes) { | ||
const basePerf = perfResults[_baseKey][perfType] | ||
this[perfType] = keys.reduce((acc, key) => { | ||
const perfResult = perfResults[key] | ||
key = kebabCase(key) | ||
return Object.assign(acc, { | ||
[key]: perfResult[perfType].toFixed(2) + 'ms', | ||
...(key === baseKey | ||
? null | ||
: { | ||
[`perf ${key}`]: this.perf(basePerf, perfResult[perfType]), | ||
}), | ||
}) | ||
}, {}) | ||
} | ||
} | ||
|
||
/** | ||
* @param {number} a | ||
* @param {number} b | ||
* @returns {string} perf description | ||
*/ | ||
perf(a, b) { | ||
return a === b | ||
? 'same' | ||
: a > b | ||
? (a / b).toFixed(2) + 'x slower' | ||
: (b / a).toFixed(2) + 'x faster' | ||
} | ||
} | ||
|
||
console.table( | ||
new Benchmark({ | ||
synckit: perfCase('synckit'), | ||
syncThreads: perfCase('sync-threads'), | ||
deasync: perfCase('deasync'), | ||
native: perfCase('native'), | ||
}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
$ node benchmarks/benchmark.cjs | ||
┌───────────┬────────────┬──────────────┬───────────────────┬────────────┬────────────────┬───────────┬─────────────────┐ | ||
│ (index) │ synckit │ sync-threads │ perf sync-threads │ deasync │ perf deasync │ native │ perf native │ | ||
├───────────┼────────────┼──────────────┼───────────────────┼────────────┼────────────────┼───────────┼─────────────────┤ | ||
│ loadTime │ '26.45ms' │ '1.61ms' │ '16.47x slower' │ '15.39ms' │ '1.72x slower' │ '0.35ms' │ '76.64x slower' │ | ||
│ runTime │ '256.15ms' │ '4884.80ms' │ '19.07x faster' │ '428.31ms' │ '1.67x faster' │ '35.98ms' │ '7.12x slower' │ | ||
│ totalTime │ '282.60ms' │ '4886.41ms' │ '17.29x faster' │ '443.70ms' │ '1.57x faster' │ '36.32ms' │ '7.78x slower' │ | ||
└───────────┴────────────┴──────────────┴───────────────────┴────────────┴────────────────┴───────────┴─────────────────┘ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
$ node benchmarks/benchmark | ||
$ node benchmarks/benchmark.js | ||
┌───────────┬────────────┬──────────────┬───────────────────┬────────────┬────────────────┬───────────┬─────────────────┐ | ||
│ (index) │ synckit │ sync-threads │ perf sync-threads │ deasync │ perf deasync │ native │ perf native │ | ||
├───────────┼────────────┼──────────────┼───────────────────┼────────────┼────────────────┼───────────┼─────────────────┤ | ||
│ loadTime │ '23.78ms' │ '1.34ms' │ '17.73x slower' │ '12.33ms' │ '1.93x slower' │ '0.32ms' │ '74.50x slower' │ | ||
│ runTime │ '202.66ms' │ '4184.60ms' │ '20.65x faster' │ '307.03ms' │ '1.51x faster' │ '30.71ms' │ '6.60x slower' │ | ||
│ totalTime │ '226.44ms' │ '4185.95ms' │ '18.49x faster' │ '319.36ms' │ '1.41x faster' │ '31.02ms' │ '7.30x slower' │ | ||
│ loadTime │ '41.02ms' │ '1.93ms' │ '21.31x slower' │ '14.68ms' │ '2.80x slower' │ '0.78ms' │ '52.48x slower' │ | ||
│ runTime │ '278.47ms' │ '4819.02ms' │ '17.31x faster' │ '420.87ms' │ '1.51x faster' │ '38.73ms' │ '7.19x slower' │ | ||
│ totalTime │ '319.49ms' │ '4820.94ms' │ '15.09x faster' │ '435.55ms' │ '1.36x faster' │ '39.51ms' │ '8.09x slower' │ | ||
└───────────┴────────────┴──────────────┴───────────────────┴────────────┴────────────────┴───────────┴─────────────────┘ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const { createSyncFn } = require('../lib/index.cjs') | ||
|
||
/** | ||
* @type {() => string} | ||
*/ | ||
const syncFn = createSyncFn(require.resolve('./synckit.worker.cjs')) | ||
|
||
module.exports = syncFn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
const { createSyncFn } = require('../lib') | ||
import { createRequire } from 'module' | ||
|
||
import { createSyncFn } from '../lib/index.js' | ||
|
||
const cjsRequire = createRequire(import.meta.url) | ||
|
||
/** | ||
* @type {() => string} | ||
*/ | ||
const syncFn = createSyncFn(require.resolve('./synckit.worker')) | ||
const syncFn = createSyncFn(cjsRequire.resolve('./synckit.worker')) | ||
|
||
module.exports = syncFn | ||
export default syncFn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const fs = require('fs') | ||
|
||
const { runAsWorker } = require('../lib/index.cjs') | ||
|
||
runAsWorker(filename => fs.promises.readFile(filename, 'utf8')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
const fs = require('fs') | ||
import fs from 'fs' | ||
|
||
const { runAsWorker } = require('../lib') | ||
import { runAsWorker } from '../lib/index.js' | ||
|
||
runAsWorker(filename => fs.promises.readFile(filename, 'utf8')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
{ | ||
"name": "synckit", | ||
"version": "0.3.4", | ||
"type": "module", | ||
"description": "Perform async work synchronously in Node.js using `worker_threads`, or `child_process` as fallback, with first-class TypeScript support.", | ||
"repository": "git+https://github.com/rx-ts/synckit.git", | ||
"author": "JounQin <[email protected]>", | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=8.10" | ||
"node": ">=12" | ||
}, | ||
"exports": { | ||
"import": "./lib/index.js", | ||
"require": "./lib/index.cjs" | ||
}, | ||
"main": "lib", | ||
"module": "lib/es2015", | ||
"types": "lib", | ||
"files": [ | ||
"lib", | ||
|
@@ -25,44 +28,51 @@ | |
"synckit" | ||
], | ||
"scripts": { | ||
"benchmark": "node benchmarks/benchmark", | ||
"benchmark:export": "yarn benchmark > benchmarks/benchmark.txt", | ||
"benchmark": "run-s benchmark:*", | ||
"benchmark-export": "run-s benchmark-export:*", | ||
"benchmark-export:cjs": "yarn benchmark:cjs > benchmarks/benchmark.cjs.txt", | ||
"benchmark-export:esm": "yarn benchmark:esm> benchmarks/benchmark.esm.txt", | ||
"benchmark:cjs": "node benchmarks/benchmark.cjs", | ||
"benchmark:esm": "node benchmarks/benchmark.js", | ||
"build": "run-p build:*", | ||
"build:r": "r -f es2015", | ||
"build:r": "r -f cjs", | ||
"build:ts": "tsc -p src", | ||
"jest": "node --experimental-vm-modules node_modules/.bin/jest --setupFiles dotenv/config", | ||
"lint": "run-p lint:*", | ||
"lint:es": "eslint . --cache -f friendly --max-warnings 10", | ||
"lint:tsc": "tsc --noEmit", | ||
"prepare": "simple-git-hooks && yarn-deduplicate --strategy fewer || exit 0", | ||
"prerelease": "npm run build", | ||
"pretest": "yarn build:ts", | ||
"release": "clean-publish && changeset publish", | ||
"test": "jest", | ||
"test-worker": "ts-node test/test-worker-ts && node test/test-worker", | ||
"test": "yarn jest", | ||
"typecov": "type-coverage" | ||
}, | ||
"dependencies": { | ||
"tslib": "^2.3.0", | ||
"tslib": "^2.3.1", | ||
"uuid": "^8.3.2" | ||
}, | ||
"devDependencies": { | ||
"@1stg/lib-config": "^3.0.0", | ||
"@1stg/lib-config": "^4.0.0", | ||
"@changesets/changelog-github": "^0.4.0", | ||
"@changesets/cli": "^2.16.0", | ||
"@types/jest": "^26.0.24", | ||
"@types/node": "^16.3.1", | ||
"@types/jest": "^27.0.1", | ||
"@types/node": "^16.7.5", | ||
"@types/uuid": "^8.3.1", | ||
"clean-publish": "^2.1.1", | ||
"deasync": "^0.1.21", | ||
"deasync": "^0.1.23", | ||
"enhanced-resolve": "^5.8.2", | ||
"postcss": "^8.3.6", | ||
"sync-threads": "^1.0.1", | ||
"ts-expect": "^1.3.0", | ||
"ts-jest": "^27.0.3", | ||
"ts-node": "^10.1.0", | ||
"type-coverage": "^2.17.3", | ||
"typescript": "^4.2.4" | ||
"ts-jest": "^27.0.5", | ||
"ts-node": "^10.2.1", | ||
"type-coverage": "^2.18.1", | ||
"typescript": "^4.4.2" | ||
}, | ||
"resolutions": { | ||
"prettier": "^2.3.2" | ||
"prettier": "^2.3.2", | ||
"tslib": "^2.3.1" | ||
}, | ||
"commitlint": { | ||
"extends": "@1stg" | ||
|
@@ -71,8 +81,20 @@ | |
"preset": "ts-jest", | ||
"testEnvironment": "node", | ||
"collectCoverage": true, | ||
"extensionsToTreatAsEsm": [ | ||
".ts" | ||
], | ||
"moduleNameMapper": { | ||
"^(\\.{1,2}/.*)\\.js$": "$1", | ||
"^synckit$": "<rootDir>/src" | ||
}, | ||
"globals": { | ||
"ts-jest": { | ||
"useESM": true, | ||
"tsconfig": { | ||
"importHelpers": false | ||
} | ||
} | ||
} | ||
}, | ||
"prettier": "@1stg/prettier-config", | ||
|
Oops, something went wrong.