Skip to content

Commit

Permalink
perf: do not use assert() in internal pure engine code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Sep 11, 2024
1 parent b28d780 commit 179d4cc
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/engine.pure.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ let willstart

const abstractProcess = globalThis.process || globalThis.EXODUS_TEST_PROCESS

// assert module is slower
const check = (condition, message) => {
if (!condition) throw new Error(message || 'Unexpected')
}

function parseArgs(args) {
assert(args.length <= 3)
check(args.length <= 3)
const name = typeof args[0] === 'string' ? args.shift() : 'test'
const fn = args.pop()
const options = args.pop() || {}
Expand All @@ -38,7 +43,7 @@ class Context {
if (this.root) {
this.parent.children.push(this)
} else {
assert(this.name === '<root>' && !this.parent)
check(this.name === '<root>' && !this.parent)
this.root = this
}

Expand All @@ -60,13 +65,13 @@ class Context {
}

function enterContext(name, options) {
assert(!running)
check(!running)
if (willstart) clearTimeout(willstart) // have to he accurate for engines like Hermes
context = new Context(context, name, options)
}

function exitContext() {
assert(context !== context.root)
check(context !== context.root)
context = context.parent
if (context === context.root) willstart = setTimeout(run, 0)
}
Expand All @@ -80,10 +85,10 @@ const runOnly = process.env.EXODUS_TEST_ONLY === '1'

async function runContext(context) {
const { options, children, hooks, fn } = context
assert(!context.running, 'Can not run twice')
check(!context.running, 'Can not run twice')
// eslint-disable-next-line @exodus/mutable/no-param-reassign-prop-only
context.running = true
assert(children.length === 0 || !fn)
check(children.length === 0 || !fn)
if (options.skip) return print('⏭ SKIP', context.fullName)
if (context.fn) {
if (runOnly) {
Expand Down Expand Up @@ -139,9 +144,9 @@ async function runContext(context) {
}

async function run() {
assert(!running)
check(!running)
running = true
assert(context === context.root)
check(context === context.root)
await runContext(context).catch((error) => {
// Should not throw under regular circumstances
print('‼ FATAL', error)
Expand Down Expand Up @@ -207,7 +212,7 @@ class MockTimers {
#elapsed = 0
#queue = []
enable({ now = 0, apis = ['setInterval', 'setTimeout', 'setImmediate', 'Date'] } = {}) {
assert(!this.#enabled, 'MockTimers is already enabled!')
check(!this.#enabled, 'MockTimers is already enabled!')
this.#base = +now
this.#elapsed = 0
if (apis.includes('setInterval')) {
Expand Down

0 comments on commit 179d4cc

Please sign in to comment.