Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,6 @@
toString = objectProto.toString,
unshift = arrayRef.unshift;

/** Used to avoid inclusion in Browserified bundles. */
var req = require;

/** Detect DOM document object. */
var doc = isHostType(context, 'document') && context.document;

Expand Down Expand Up @@ -428,6 +425,16 @@
*/
support.timeout = isHostType(context, 'setTimeout') && isHostType(context, 'clearTimeout');

/**
* Test if able to run a snippet of JavaScript via script injection.
*
* @memberOf Benchmark.support
* @type boolean
*/
support.canInjectScript = freeDefine
? (root.define.amd !== undefined)
: (root.Benchmark !== undefined);

/**
* Detect if function decompilation is support.
*
Expand Down Expand Up @@ -722,7 +729,7 @@
};
// Fix JaegerMonkey bug.
// For more information see http://bugzil.la/639720.
createFunction = support.browser && (createFunction('', 'return"' + uid + '"') || noop)() == uid ? createFunction : Function;
createFunction = support.browser && support.canInjectScript && (createFunction('', 'return"' + uid + '"') || noop)() == uid ? createFunction : Function;
return createFunction.apply(null, arguments);
}

Expand Down Expand Up @@ -858,14 +865,27 @@

/**
* A wrapper around `require` to suppress `module missing` errors.
* Used to avoid inclusion in Browserified bundles.
*
* @private
* @param {string} id The module id.
* @returns {*} The exported module or `null`.
*/
function require(id) {
function req(id) {
try {
var result = freeExports && freeRequire(id);
var result;
if (freeExports && freeRequire) {
switch (id) {
case 'platform':
result = require('platform');
break;
default:
/** Used to avoid inclusion in Browserified bundles. */
// eg: microtime
result = freeRequire(id);
break;
}
}
} catch (e) { }
return result || null;
}
Expand Down Expand Up @@ -2359,7 +2379,7 @@
else {
// Fix TraceMonkey bug associated with clock fallbacks.
// For more information see http://bugzil.la/509069.
if (support.browser) {
if (support.browser && support.canInjectScript) {
runScript(uid + '=1;delete ' + uid);
}
// We're done.
Expand Down Expand Up @@ -2566,7 +2586,7 @@
* @memberOf Benchmark
* @type Object
*/
'platform': context.platform || require('platform') || ({
'platform': context.platform || req('platform') || ({
'description': context.navigator && context.navigator.userAgent || null,
'layout': null,
'product': null,
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ declare namespace Benchmark {

export interface Support {
browser: boolean;
canInjectScript: boolean;
timeout: boolean;
decompilation: boolean;
}
Expand Down