forked from isaacs/sax-js
-
Notifications
You must be signed in to change notification settings - Fork 77
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
~30-40% perf win using 'charCodeAt' in CParser.write() #57
Merged
+437
−76
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2689bf9
Benchmark.js based benchmark to compare perf w/latest published version
DLehenbauer f057786
~30-40% perf win using charCode instead of strings
DLehenbauer 9eedc9e
add easy to debug test cases with VS Code config
DLehenbauer 445b19d
Lint
DLehenbauer e85bc3f
Lint 2
DLehenbauer 3f703c9
Remove /.vscode and add to .gitignore
DLehenbauer fb3ed03
Remove unused Char constants
DLehenbauer ed69aca
Benchmark: remove dependency on 'npm-install-version'
DLehenbauer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.DS_Store | ||
node_modules/ | ||
*.log | ||
/.vscode |
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,48 @@ | ||
/* eslint-disable no-console */ | ||
const { Suite } = require("benchmark"); | ||
const { Listener } = require("./listener"); | ||
const oldClarinet = require("clarinet"); | ||
const newClarinet = require(".."); | ||
|
||
const oldParser = oldClarinet.parser(); | ||
const oldListener = new Listener(oldParser); | ||
|
||
const newParser = newClarinet.parser(); | ||
const newListener = new Listener(newParser); | ||
|
||
const suites = | ||
["creationix", "npm", "twitter", "wikipedia"] | ||
.map((name) => { | ||
// eslint-disable-next-line security/detect-non-literal-require | ||
return { name, json: JSON.stringify(require(`../samples/${name}.json`)) }; | ||
}); | ||
|
||
for (const { name, json } of suites) { | ||
new Suite("name") | ||
// Uncomment the below to add node's native JSON parsing to the results: | ||
// .add(`native-${name}`, () => JSON.parse(json)) | ||
.add(`old-${name}`, () => { | ||
oldListener.reset(); | ||
oldParser.write(json); | ||
oldParser.close(); | ||
oldListener.check(); | ||
}) | ||
.add(`new-${name}`, () => { | ||
newListener.reset(); | ||
newParser.write(json); | ||
newParser.close(); | ||
newListener.check(); | ||
}) | ||
.on("cycle", (event) => { | ||
console.log(String(event.target)); | ||
}) | ||
.on("error", (event) => { | ||
console.error(String(event.target.error)); | ||
}) | ||
.on("complete", (event) => { | ||
console.log( | ||
`Fastest is ${event.currentTarget.filter("fastest").map("name")}\n` | ||
); | ||
}) | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const assert = require("assert"); | ||
|
||
/** | ||
* Listener that counts all events emitted by the Clarinet.js parser and sanity checks the totals. | ||
* This defeats potential dead code elimination and helps make the benchmark more realistic. | ||
*/ | ||
class Listener { | ||
constructor(parser) { | ||
this.reset(); | ||
|
||
parser.onready = () => { | ||
this.ready++; | ||
}; | ||
|
||
parser.onopenobject = (name) => { | ||
this.openObject++; | ||
typeof name === "undefined" || parser.onkey(name); | ||
}; | ||
|
||
parser.onkey = (name) => { | ||
this.key++; | ||
assert(name !== "𝓥𝓸𝓵𝓭𝓮𝓶𝓸𝓻𝓽"); | ||
}; | ||
|
||
parser.oncloseobject = () => { | ||
this.closeObject++; | ||
}; | ||
|
||
parser.onopenarray = () => { | ||
this.openArray++; | ||
}; | ||
|
||
parser.onclosearray = () => { | ||
this.closeArray++; | ||
}; | ||
|
||
parser.onvalue = () => { | ||
this.value++; | ||
}; | ||
|
||
parser.onerror = () => { | ||
this.error++; | ||
}; | ||
|
||
parser.onend = () => { | ||
this.end++; | ||
}; | ||
} | ||
|
||
/** Resets the counts between iterations. */ | ||
reset() { | ||
this.ready = 0; | ||
this.openObject = 0; | ||
this.key = 0; | ||
this.closeObject = 0; | ||
this.openArray = 0; | ||
this.closeArray = 0; | ||
this.value = 0; | ||
this.error = 0; | ||
this.end = 0; | ||
} | ||
|
||
/** Sanity checks the total event counts. */ | ||
check() { | ||
assert(this.ready === 1); | ||
assert(this.end === 1); | ||
assert(this.error === 0); | ||
assert(this.value + this.openObject + this.openArray >= this.key); | ||
assert(this.openObject === this.closeObject); | ||
assert(this.openArray === this.closeArray); | ||
} | ||
} | ||
|
||
module.exports = { Listener }; |
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,26 @@ | ||
{ | ||
"name": "clarinet-benchmark", | ||
"description": "Benchmark to measure clarinet.js vs. the last published version", | ||
"version": "0.1.0", | ||
"main": "./index.js", | ||
"homepage": "https://github.com/dscape/clarinet", | ||
"private": true, | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/dscape/clarinet.git" | ||
}, | ||
"bugs": { | ||
"url": "http://github.com/dscape/clarinet/issues" | ||
}, | ||
"devDependencies": { | ||
"benchmark": "^2.1.4", | ||
"mocha": "1.3.x" | ||
}, | ||
"scripts": { | ||
"test": "node index.js" | ||
}, | ||
"license": "BSD-2-Clause", | ||
"dependencies": { | ||
"clarinet": "^0.12.1" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the benchmark I used to measure the impact of the change. It measures the four .json files under "../samples" and for me shows ~30+% speedup under 'node 8.11.4'. The speedup was significant enough that I didn't bother disabling turbo boost, sleep states, etc. on the CPU.