Skip to content

Commit 6954d75

Browse files
committed
Switch setImmediate implementation from using MessageChannel as an async deferral to Promise.resolve().then(). This is [much faster](https://esbench.com/bench/55d4d44e2efbca1100bf7251), *however* it comes with the caveat of being unusable for animation. While this matches the use-case for setImmediate() within Preact, which is simply to debounce rendering, it means that componentDidUpdate() and setState() callbacks should no longer be used as a means of animation. This is only true when using the built-in debounce mechanism, overriding to requestAnimationFrame is still sufficient for all other cases.
1 parent 0f1eeb0 commit 6954d75

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

src/util.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EMPTY_BASE, NON_DIMENSION_PROPS } from './constants';
1+
import { NON_DIMENSION_PROPS } from './constants';
22

33

44
/** Copy own-properties from `props` onto `obj`.
@@ -133,13 +133,8 @@ export const toLowerCase = memoize( s => s.toLowerCase() );
133133
// For animations, rAF is vastly superior. However, it scores poorly on benchmarks :(
134134
// export const setImmediate = typeof requestAnimationFrame==='function' ? requestAnimationFrame : setTimeout;
135135

136-
let ch;
137-
try { ch = new MessageChannel(); } catch (e) {}
138-
139136
/** Call a function asynchronously, as soon as possible.
140137
* @param {Function} callback
141138
*/
142-
export const setImmediate = ch ? ( f => {
143-
ch.port1.onmessage = f;
144-
ch.port2.postMessage(EMPTY_BASE);
145-
}) : setTimeout;
139+
let resolved = typeof Promise!=='undefined' && Promise.resolve();
140+
export const setImmediate = resolved ? (f => { resolved.then(f); }) : setTimeout;

0 commit comments

Comments
 (0)