Optimize typing a single character#1233
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
|
The initial Could you verify that resolving the promise on a new event loop step carries that performance hit? |
| export async function wait(config: Instance['config']): Promise<void> { | ||
| const delay = config.delay | ||
| if (typeof delay !== 'number') { | ||
| return | ||
| } | ||
| return Promise.all([ | ||
| await Promise.all([ |
There was a problem hiding this comment.
This makes the function return Promise<undefined> instead of undefined for delay: null and results in await wait() to add a new micro task.
Are we sure this has no effect?
There was a problem hiding this comment.
Doesn't await wait() always create a new microtask regardless? Doesn't matter what wait() returns, right?
There was a problem hiding this comment.
I'm not sure. I guess it depends on implementation.
Typescript reports 'await' has no effect on the type of this expression. ts(80007) if you try await undefined.
The following results in sync in node@10 and async in node@12:
async function waitAsync() {
return undefined
}
function waitSync() {
return undefined
}
(async function () {
await Promise.race([
(async () => {
await waitAsync()
return 'async'
})(),
(async () => {
await waitSync()
return 'sync'
})(),
]).then(console.log)
})()If we're not sure that this has no effect on any platform that is still supported, I wouldn't touch it if I don't have to.
What:
This improves performance of typing a single character by ~2x
Why:
Users complain about performance #577. Also related to this thread
How:
The desired behavior is to (I suppose) yield to the next macrotask in between keyboard actions. We can avoid the initial
wait. Some quick benchmarking suggested thosewaitcalls make up the bulk of the runtime of this function. When there is only 1 action to run, this brings thewaitcalls from 2 => 1Checklist:
I don't believe documentation/tests would be required here.