-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a benchmark to measure proxy overhead (around 4x slower than direct access), and add a test to ensure that overall performance is not too much impacted.
- Loading branch information
Showing
5 changed files
with
110 additions
and
2 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
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,50 @@ | ||
'use strict' | ||
|
||
const benchmark = require('benchmark') | ||
const list = [1, 2, 3, 4, 5] | ||
|
||
const sum = () => { | ||
let total = 0 | ||
for (let i = 0; i < list.length; i++) { | ||
total += list[i] | ||
} | ||
return total | ||
} | ||
|
||
// simple object | ||
const obj = {sum} | ||
|
||
// proxy that wraps an empty object | ||
const emptyProxy = new Proxy({}, { | ||
get(target, key) { | ||
if (key === 'sum') { | ||
return obj.sum | ||
} | ||
return undefined // eslint-disable-line no-undefined | ||
} | ||
}) | ||
|
||
// no-op proxy around | ||
const noopProxy = new Proxy(obj, {}) | ||
|
||
// one-time proxy | ||
const handler = { | ||
get(target, key) { | ||
target.sum = sum | ||
delete handler.get | ||
return target[key] | ||
} | ||
} | ||
const oneTimeProxy = new Proxy({}, handler) | ||
|
||
new benchmark.Suite().add('normal method', () => | ||
obj.sum() | ||
).add('proxy', () => | ||
emptyProxy.sum() | ||
).add('no-op proxy', () => | ||
noopProxy.sum() | ||
).add('one time proxy', () => | ||
oneTimeProxy.sum() | ||
).on('cycle', ({target}) => | ||
console.log(target.toString()) // eslint-disable-line no-console | ||
).run() |
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
File renamed without changes.