@callstack/reassure-measure
Version:
Performance measurement library for React and React Native
45 lines (41 loc) • 2.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.findOutliers = findOutliers;
var math = _interopRequireWildcard(require("mathjs"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/* Adapted from https://github.com/sharkdp/hyperfine/blob/3b0918511aee4d6f8860bb663cb7a7af57bc3814/src/outlier_detection.rs */
// Minimum modified Z-score for a datapoint to be an outlier. Here, 1.4826 is a factor that
// converts the MAD to an estimator for the standard deviation. The second factor is the number
// of standard deviations.
const OUTLIER_THRESHOLD = 1.4826 * 10;
function findOutliers(items) {
if (items.length <= 1) {
return {
results: items,
outliers: []
};
}
const durations = items.map(({
duration
}) => duration);
// Compute the sample median and median absolute deviation (MAD)
const median = math.median(durations);
const mad = math.mad(durations);
return items.reduce((acc, result) => {
const modifiedZScore = (result.duration - median) / (mad > 0 ? mad : Number.EPSILON);
// An outlier is a point that is larger than the modified Z-score threshold
if (Math.abs(modifiedZScore) > OUTLIER_THRESHOLD) {
acc.outliers.push(result);
} else {
acc.results.push(result);
}
return acc;
}, {
results: [],
outliers: []
});
}
//# sourceMappingURL=outlier-helpers.js.map