Add experimental safariYieldStrategy: "time" config option

This greatly improves speed, at least in jasmine-core's own tests.
This commit is contained in:
Steve Gravrock
2025-11-12 21:08:59 -08:00
parent 7b2807b321
commit 9c2ffae2f9
7 changed files with 224 additions and 36 deletions

View File

@@ -151,7 +151,22 @@ getJasmineRequireObj().Configuration = function(j$) {
* @type number
* @default 0
*/
extraDescribeStackFrames: 0
extraDescribeStackFrames: 0,
/**
* The strategy to use in Safari and similar browsers to determine how often
* to yield control by calling setTimeout. If set to "count", the default,
* the frequency of setTimeout calls is based on the number of relevant
* function calls. If set to "time", the frequency of setTimeout calls is
* based on elapsed time. Using "time" may provide a significant performance
* improvement, but as of 6.0 it hasn't been tested with a wide variety of
* workloads and should be considered experimental.
* @name Configuration#safariYieldStrategy
* @since 6.0.0
* @type 'count' | 'time'
* @default 'count'
*/
safariYieldStrategy: 'count'
};
Object.freeze(defaultConfig);
@@ -212,6 +227,18 @@ getJasmineRequireObj().Configuration = function(j$) {
this.#values.extraDescribeStackFrames =
changes.extraDescribeStackFrames;
}
if (typeof changes.safariYieldStrategy !== 'undefined') {
const v = changes.safariYieldStrategy;
if (v === 'count' || v === 'time') {
this.#values.safariYieldStrategy = v;
} else {
throw new Error(
"Invalid safariYieldStrategy value. Valid values are 'count' and 'time'."
);
}
}
}
}

View File

@@ -99,6 +99,7 @@ getJasmineRequireObj().Env = function(j$) {
config.update(changes);
deprecator.verboseDeprecations(config.verboseDeprecations);
stackClearer.setSafariYieldStrategy(config.safariYieldStrategy);
};
/**

View File

@@ -2,21 +2,46 @@ getJasmineRequireObj().StackClearer = function(j$) {
'use strict';
const maxInlineCallCount = 10;
// 25ms gives a good balance of speed and UI responsiveness when running
// jasmine-core's own tests in Safari 18. The exact value isn't critical.
const safariYieldIntervalMs = 25;
function browserQueueMicrotaskImpl(global) {
const unclampedSetTimeout = getUnclampedSetTimeout(global);
const { queueMicrotask } = global;
let currentCallCount = 0;
let yieldStrategy = 'count';
let currentCallCount = 0; // for count strategy
let nextSetTimeoutTime; // for time strategy
return {
clearStack(fn) {
currentCallCount++;
let shouldSetTimeout;
if (currentCallCount < maxInlineCallCount) {
queueMicrotask(fn);
if (yieldStrategy === 'time') {
const now = new Date().getTime();
shouldSetTimeout = now >= nextSetTimeoutTime;
if (shouldSetTimeout) {
nextSetTimeoutTime = now + safariYieldIntervalMs;
}
} else {
currentCallCount = 0;
shouldSetTimeout = currentCallCount >= maxInlineCallCount;
if (shouldSetTimeout) {
currentCallCount = 0;
}
}
if (shouldSetTimeout) {
unclampedSetTimeout(fn);
} else {
queueMicrotask(fn);
}
},
setSafariYieldStrategy(strategy) {
yieldStrategy = strategy;
if (yieldStrategy === 'time') {
nextSetTimeoutTime = new Date().getTime() + safariYieldIntervalMs;
}
}
};
@@ -28,7 +53,8 @@ getJasmineRequireObj().StackClearer = function(j$) {
return {
clearStack(fn) {
queueMicrotask(fn);
}
},
setSafariYieldStrategy() {}
};
}
@@ -48,7 +74,8 @@ getJasmineRequireObj().StackClearer = function(j$) {
currentCallCount = 0;
setTimeout(fn);
}
}
},
setSafariYieldStrategy() {}
};
}