Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Improve performance #69

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { format } = require('util')

function build () {
const codes = {}
const emitted = new Map()
const emitted = new Set()

function create (name, code, message) {
jsumners marked this conversation as resolved.
Show resolved Hide resolved
if (!name) throw new Error('Warning name must not be empty')
Expand Down Expand Up @@ -37,16 +37,15 @@ function build () {
}
}

emitted.set(code, false)
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
codes[code] = buildWarnOpts

return codes[code]
}

function emit (code, a, b, c) {
if (codes[code] === undefined) throw new Error(`The code '${code}' does not exist`)
if (emitted.get(code) === true) return
emitted.set(code, true)
if (emitted.has(code)) return
emitted.add(code)

const warning = codes[code](a, b, c)
process.emitWarning(warning.message, warning.name, warning.code)
Expand Down
2 changes: 1 addition & 1 deletion test/emit-interpolated-string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('emit with interpolated string', t => {
t.equal(warning.name, 'FastifyDeprecation')
t.equal(warning.code, 'CODE')
t.equal(warning.message, 'Hello world')
t.ok(emitted.get('CODE'))
t.ok(emitted.has('CODE'))
}

create('FastifyDeprecation', 'CODE', 'Hello %s')
Expand Down
2 changes: 1 addition & 1 deletion test/emit-once-only.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('emit should emit a given code only once', t => {
t.equal(warning.name, 'FastifyDeprecation')
t.equal(warning.code, 'CODE')
t.equal(warning.message, 'Hello world')
t.ok(emitted.get('CODE'))
t.ok(emitted.has('CODE'))
}

create('FastifyDeprecation', 'CODE', 'Hello world')
Expand Down
2 changes: 1 addition & 1 deletion test/jest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('works with jest', done => {
// we can only test it was emitted indirectly
// and test no exception is raised
setImmediate(() => {
expect(emitted.get('CODE')).toBeTruthy()
expect(emitted.has('CODE')).toBeTruthy()
done()
})
})