Skip to content

Commit

Permalink
update: intermediary step to avoid dead-code elimination
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelGSS committed Sep 2, 2024
1 parent da5527e commit 72810cd
Show file tree
Hide file tree
Showing 28 changed files with 197 additions and 118 deletions.
5 changes: 5 additions & 0 deletions bench/add-property.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const suite = createBenchmarkSuite('Adding property')
suite
.add('Directly in the object', function () {
const data = { test: 'Hello' }
return data
})
.add('Using dot notation', function () {
const data = {}

data.test = 'Hello'
return data
})
.add('Using define property (writable)', function () {
const data = {}
Expand All @@ -20,6 +22,7 @@ suite
enumerable: true,
configurable: true,
})
return data
})
.add('Using define property initialized (writable)', function () {
const data = { test: undefined }
Expand All @@ -29,6 +32,7 @@ suite
enumerable: true,
configurable: true,
})
return data
})
.add('Using define property (getter)', function () {
const data = {}
Expand All @@ -40,6 +44,7 @@ suite
enumerable: true,
configurable: true,
})
return data
})

await suite.runAndPrintResults()
4 changes: 2 additions & 2 deletions bench/array-creation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const suite = createBenchmarkSuite('Array Creation')

suite
.add('new Array', function () {
new Array(1024 * 1024)
return new Array(1024 * 1024)
})
.add('Array.from', function () {
Array.from({ length: 1024 * 1024 })
return Array.from({ length: 1024 * 1024 })
})

await suite.runAndPrintResults()
8 changes: 4 additions & 4 deletions bench/blob.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ const blob1024 = new Blob(source1024)
suite
.add('new Blob (128)', function () {
const result = new Blob(source128)
assert.ok(result)
return result
})
.add('new Blob (1024)', function () {
const result = new Blob(source1024)
assert.ok(result)
return result
})
.add(
'text (128)',
Expand Down Expand Up @@ -48,11 +48,11 @@ suite
)
.add('slice (0, 64)', function () {
const blob = blob128.slice(0, 64)
assert.ok(blob)
return blob
})
.add('slice (0, 512)', function (deferred) {
const blob = blob1024.slice(0, 512)
assert.ok(blob)
return blob
})

await suite.runAndPrintResults()
6 changes: 6 additions & 0 deletions bench/compare-using-instanceof.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,42 @@ suite
if (err instanceof Error) {
1 + 1
}
return err
})
.add('[True conditional] Using constructor name', function () {
const err = new Error()
if (err.constructor.name === 'Error') {
1 + 1
}
return err
})
.add('[True conditional] Check if property is valid then instanceof ', function () {
const err = new Error()
if (err && err instanceof Error) {
1 + 1
}
return err
})
.add('[False conditional] Using instanceof only', function () {
const err = undefined
if (err instanceof Error) {
1 + 1
}
return err
})
.add('[False conditional] Using constructor name', function () {
const err = undefined
if (err?.constructor?.name === 'Error') {
1 + 1
}
return err
})
.add('[False conditional] Check if property is valid then instanceof ', function () {
const err = undefined
if (err && err instanceof Error) {
1 + 1
}
return err
})

await suite.runAndPrintResults()
14 changes: 7 additions & 7 deletions bench/date-format.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ const df = new Intl.DateTimeFormat()

suite
.add('Intl.DateTimeFormat().format(Date.now())', function () {
new Intl.DateTimeFormat().format(Date.now())
return new Intl.DateTimeFormat().format(Date.now())
})
.add('Intl.DateTimeFormat().format(new Date())', function () {
new Intl.DateTimeFormat().format(new Date())
return new Intl.DateTimeFormat().format(new Date())
})
.add('Intl.DateTimeFormat(undefined, twoDigitsLocaleOptions).format(Date.now())', function () {
new Intl.DateTimeFormat(undefined, twoDigitsLocaleOptions).format(Date.now())
return new Intl.DateTimeFormat(undefined, twoDigitsLocaleOptions).format(Date.now())
})
.add('Intl.DateTimeFormat(undefined, twoDigitsLocaleOptions).format(new Date())', function () {
new Intl.DateTimeFormat(undefined, twoDigitsLocaleOptions).format(new Date())
return new Intl.DateTimeFormat(undefined, twoDigitsLocaleOptions).format(new Date())
})
.add('Reusing Intl.DateTimeFormat()', function () {
df.format(Date.now())
return df.format(Date.now())
})
.add('Date.toLocaleDateString()', function () {
new Date().toLocaleDateString()
return new Date().toLocaleDateString()
})
.add('Date.toLocaleDateString(undefined, twoDigitsLocaleOptions)', function () {
new Date().toLocaleDateString(undefined, twoDigitsLocaleOptions)
return new Date().toLocaleDateString(undefined, twoDigitsLocaleOptions)
})

await suite.runAndPrintResults()
4 changes: 4 additions & 0 deletions bench/date-string-coersion.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ suite
.add('Using String()', function () {
const date = new Date()
const value = String(date)
return value
})
.add('Using brackets {}', function () {
const date = new Date()
const value = `${date}`
return value
})
.add("Using '' + ", function () {
const date = new Date()
const value = '' + date
return value
})
.add('Using date.toString()', function () {
const date = new Date()
const value = date.toString()
return value
})

await suite.runAndPrintResults()
6 changes: 6 additions & 0 deletions bench/deleting-properties.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ suite
data.x
data.y
data.z
return data
})
.add('Using delete property (proto: null)', function () {
const data = { __proto__: null, x: 1, y: 2, z: 3 }
Expand All @@ -21,6 +22,7 @@ suite
data.x
data.y
data.z
return data
})
.add('Using delete property (cached proto: null)', function () {
const data = new NullObject()
Expand All @@ -34,6 +36,7 @@ suite
data.x
data.y
data.z
return data
})
.add('Using undefined assignment', function () {
const data = { x: 1, y: 2, z: 3 }
Expand All @@ -42,6 +45,7 @@ suite
data.x
data.y
data.z
return data
})
.add('Using undefined assignment (proto: null)', function () {
const data = { __proto__: null, x: 1, y: 2, z: 3 }
Expand All @@ -50,6 +54,7 @@ suite
data.x
data.y
data.z
return data
})
.add('Using undefined property (cached proto: null)', function () {
const data = new NullObject()
Expand All @@ -63,6 +68,7 @@ suite
data.x
data.y
data.z
return data
})

await suite.runAndPrintResults()
6 changes: 3 additions & 3 deletions bench/error.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ const suite = createBenchmarkSuite('Node.js Error')

suite
.add('Error', function () {
new Error('test')
return new Error('test')
})
.add('NodeError', function () {
new TypeError('test')
return new TypeError('test')
})
.add('NodeError Range', function () {
new RangeError('test')
return new RangeError('test')
})

await suite.runAndPrintResults()
10 changes: 10 additions & 0 deletions bench/function-return.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,52 @@ suite
.add('Function returning null', function () {
const test = new Function('test', 'return null')
const a = test()
return a
})
.add('Function returning explicitly undefined', function () {
const test = new Function('test', 'return undefined')
const a = test()
return a
})
.add('Function returning implicitly undefined', function () {
const test = new Function('test', 'return')
const a = test()
return a
})
.add('Function returning string', function () {
const test = new Function('test', 'return "test"')
const a = test()
return a
})
.add('Function returning integer', function () {
const test = new Function('test', 'return 1')
const a = test()
return a
})
.add('Function returning float', function () {
const test = new Function('test', 'return 1.2')
const a = test()
return a
})
.add('Function returning functions', function () {
const test = new Function('test', 'return function() {}')
const a = test()
return a
})
.add('Function returning arrow functions', function () {
const test = new Function('test', 'return () => {}')
const a = test()
return a
})
.add('Function returning empty object', function () {
const test = new Function('test', 'return {}')
const a = test()
return a
})
.add('Function returning empty array', function () {
const test = new Function('test', 'return []')
const a = test()
return a
})

await suite.runAndPrintResults()
4 changes: 4 additions & 0 deletions bench/includes-vs-raw-comparison.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ suite
.add('using Array.includes', function () {
const httpVersion = '1.1'
const exists = ['2.0', '1.0', '1.1'].includes(httpVersion)
return exists
})
.add('using Array.includes (first item)', function () {
const httpVersion = '2.0'
const exists = ['2.0', '1.0', '1.1'].includes(httpVersion)
return exists
})
.add('Using raw comparison', function () {
const httpVersion = '1.1'
const exists = httpVersion === '2.0' || httpVersion === '1.0' || httpVersion === '1.1'
return exists
})
.add('Using raw comparison (first item)', function () {
const httpVersion = '2.0'
const exists = httpVersion === '2.0' || httpVersion === '1.0' || httpVersion === '1.1'
return exists
})

await suite.runAndPrintResults()
2 changes: 2 additions & 0 deletions bench/keys-vs-getownpropertynames.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ suite
c: false,
}
const keys = Object.keys(object)
return keys
})
.add('Using Object.getOwnPropertyNames()', function () {
const object = {
Expand All @@ -18,6 +19,7 @@ suite
c: false,
}
const keys = Object.getOwnPropertyNames(object)
return keys
})

await suite.runAndPrintResults()
12 changes: 6 additions & 6 deletions bench/last-array-item.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ const arr100_000_0 = Array.from({ length: 1000000 }, () => Math.floor(Math.rando

suite
.add('Length = 100 - Array.at', function () {
arr100.at(-1)
return arr100.at(-1)
})
.add('Length = 10_000 - Array.at', function () {
arr10_000.at(-1)
return arr10_000.at(-1)
})
.add('Length = 1_000_000 - Array.at', function () {
arr100_000_0.at(-1)
return arr100_000_0.at(-1)
})
.add('Length = 100 - Array[length - 1]', function () {
arr100[arr100.length - 1]
return arr100[arr100.length - 1]
})
.add('Length = 10_000 - Array[length - 1]', function () {
arr10_000[arr10_000.length - 1]
return arr10_000[arr10_000.length - 1]
})
.add('Length = 1_000_000 - Array[length - 1]', function () {
arr100_000_0[arr100_000_0.length - 1]
return arr100_000_0[arr100_000_0.length - 1]
})

await suite.runAndPrintResults()
10 changes: 5 additions & 5 deletions bench/object-creation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ Empty.prototype = Object.create(null)

suite
.add('Object.create(null)', function () {
Object.create(null)
return Object.create(null)
})
.add('Object.create({})', function () {
Object.create({})
return Object.create({})
})
.add('Cached Empty.prototype', function () {
new Empty()
return new Empty()
})
.add('Empty.prototype', function () {
function NE() {}
NE.prototype = Object.create(null)
new NE()
return new NE()
})
.add('Empty class', function () {
function C() {}
new C()
return new C()
})

await suite.runAndPrintResults()
Loading

0 comments on commit 72810cd

Please sign in to comment.