Skip to content
This repository has been archived by the owner on Jul 9, 2023. It is now read-only.

Commit

Permalink
remove platform (#18)
Browse files Browse the repository at this point in the history
replace lodash with cdn lodash
add chai for assert substiture for browser tests
make use of mocha and chai in index.html
  • Loading branch information
Uzlopak authored Mar 13, 2022
1 parent eca77cc commit 6f51403
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 169 deletions.
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@ A [robust](https://mathiasbynens.be/notes/javascript-benchmarking "Bulletproof J

## Installation

Benchmark.js’ only hard dependency is [lodash](https://lodash.com/).
Include [platform.js](https://mths.be/platform) to populate [Benchmark.platform](https://benchmarkjs.com/docs#platform).

In a browser:

```html
<script src="lodash.js"></script>
<script src="platform.js"></script>
<script src="benchmark.js"></script>
```

Expand All @@ -35,9 +30,7 @@ In an AMD loader:
```js
require({
'paths': {
'benchmark': 'path/to/benchmark',
'lodash': 'path/to/lodash',
'platform': 'path/to/platform'
'benchmark': 'path/to/benchmark'
}
},
['benchmark'], function(Benchmark) {/**/});
Expand Down
43 changes: 6 additions & 37 deletions benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
var contextProps = [
'Array', 'Date', 'Function', 'Math', 'Object', 'RegExp', 'String',
'clearTimeout', 'chrome', 'chromium', 'document', 'navigator',
'platform', 'process', 'runtime', 'setTimeout'
'process', 'runtime', 'setTimeout'
];

/** Used to avoid hz of Infinity. */
Expand Down Expand Up @@ -875,16 +875,9 @@
try {
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;
}
/** Used to avoid inclusion in Browserified bundles. */
// eg: microtime
result = freeRequire(id);
}
} catch (e) { }
return result || null;
Expand Down Expand Up @@ -2578,28 +2571,6 @@
'onStart': undefined
},

/**
* Platform object with properties describing things like browser name,
* version, and operating system. See [`platform.js`](https://mths.be/platform).
*
* @static
* @memberOf Benchmark
* @type Object
*/
'platform': context.platform || req('platform') || ({
'description': context.navigator && context.navigator.userAgent || null,
'layout': null,
'product': null,
'name': null,
'manufacturer': null,
'os': null,
'prerelease': null,
'version': null,
'toString': function () {
return this.description || '';
}
}),

/**
* The semantic version number.
*
Expand Down Expand Up @@ -3111,10 +3082,8 @@
// Some AMD build optimizers, like r.js, check for condition patterns like the following:
if (typeof define == 'function' && typeof define.amd == 'object' && define.amd) {
// Define as an anonymous module so, through path mapping, it can be aliased.
define(['platform'], function (platform) {
return runInContext({
'platform': platform
});
define('benchmark', function () {
return runInContext();
});
}
else {
Expand Down
17 changes: 2 additions & 15 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
* <a href="#benchmarkoptionsonerror">`Benchmark.options.onError`</a>
* <a href="#benchmarkoptionsonreset">`Benchmark.options.onReset`</a>
* <a href="#benchmarkoptionsonstart">`Benchmark.options.onStart`</a>
* <a href="#benchmarkplatform">`Benchmark.platform`</a>
* <a href="#benchmarksupport">`Benchmark.support`</a>
* <a href="#benchmarksupportbrowser">`Benchmark.support.browser`</a>
* <a href="#benchmarkversion">`Benchmark.version`</a>
Expand Down Expand Up @@ -117,8 +116,8 @@
The Benchmark constructor.
<br>
<br>
Note: The Benchmark constructor exposes a handful of lodash methods to
make working with arrays, collections, and objects easier. The lodash
Note: The Benchmark constructor exposes a handful of lodash-like methods to
make working with arrays, collections, and objects easier. The lodash-like
methods are:<br>
[`each/forEach`](https://lodash.com/docs#forEach), [`forOwn`](https://lodash.com/docs#forOwn),
[`has`](https://lodash.com/docs#has), [`indexOf`](https://lodash.com/docs#indexOf),
Expand Down Expand Up @@ -1132,18 +1131,6 @@ An event listener called when the benchmark starts running.

<!-- div -->

<h3 id="benchmarkplatform"><code>Benchmark.platform</code></h3>
[&#x24C8;](https://github.com/bestiejs/benchmark.js/blob/2.1.4/benchmark.js#L2276 "View in source") [&#x24C9;][1]

Platform object with properties describing things like browser name,
version, and operating system. See [`platform.js`](https://mths.be/platform).

---

<!-- /div -->

<!-- div -->

<h3 id="benchmarksupport"><code>Benchmark.support</code></h3>
[&#x24C8;](https://github.com/bestiejs/benchmark.js/blob/2.1.4/benchmark.js#L195 "View in source") [&#x24C9;][1]

Expand Down
3 changes: 1 addition & 2 deletions example/jsperf/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ <h2>Add a comment</h2>
&middot; by <a href="https://mathiasbynens.be/" title="Mathias Bynens, front-end web developer">@mathias</a>
</footer>

<script src="../../node_modules/lodash/lodash.js"></script>
<script src="../../node_modules/platform/platform.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
<script src="../../benchmark.js"></script>
<script src="ui.js"></script>
<script src="../../plugin/ui.browserscope.js"></script>
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"homepage": "https://benchmarkjs.com/",
"license": "MIT",
"main": "benchmark.js",
"keywords": ["benchmark", "performance", "speed"],
"keywords": [
"benchmark",
"performance",
"speed"
],
"author": "Mathias Bynens <[email protected]>",
"types": "types/index.d.ts",
"contributors": [
Expand All @@ -24,11 +28,8 @@
"test:coverage": "nyc npm run test",
"test:tsd": "tsd"
},
"dependencies": {
"lodash": "^4.17.10",
"platform": "^1.3.6"
},
"devDependencies": {
"chai": "^4.3.6",
"coveralls": "^3.1.1",
"docdown": "^0.7.3",
"mocha": "^9.2.1",
Expand Down
95 changes: 20 additions & 75 deletions test/index.html
Original file line number Diff line number Diff line change
@@ -1,82 +1,27 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Benchmark.js Test Suite</title>
<link rel="stylesheet" href="../node_modules/qunitjs/qunit/qunit.css">
</head>
<body>
<div id="qunit"></div>
<script src="../node_modules/qunitjs/qunit/qunit.js"></script>
<script src="../node_modules/qunit-extras/qunit-extras.js"></script>
<script src="../node_modules/lodash/lodash.js"></script>
<script src="../node_modules/platform/platform.js"></script>
<script>
QUnit.config.hidepassed = true;
QUnit.urlParams.norequire = /[?&]norequire=true(?:&|$)/.test(location.search);

// Load test.js if not using require.js.
document.write(QUnit.urlParams.norequire
? '<script src="../benchmark.js"><\/script><script src="test.js"><\/script>'
: '<script src="../node_modules/requirejs/require.js"><\/script>'
);
</script>
<script>
var benchmarkModule;
<head>
<meta charset="utf-8">
<title>Benchmark.js Test Suite</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/9.2.2/mocha.css">
</head>

(function() {
if (!window.require) {
return;
}
QUnit.config.autostart = false;
<body>
<div id="mocha" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/8.0.1/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.3.6/chai.min.js"></script>
<script src="../benchmark.js"></script>

requirejs.config({
'baseUrl': './',
'urlArgs': 't=' + (+new Date),
'waitSeconds': 0,
'packages': [
{
'name': 'benchmark',
'location': '..',
'main': 'benchmark'
},
{
'name': 'lodash',
'location': '../node_modules/lodash',
'main': 'lodash'
},
{
'name': 'platform',
'location': '../node_modules/platform',
'main': 'platform'
},
{
'name': 'test',
'location': '.',
'main': 'test'
}
]
});
<script>
mocha.setup('bdd');
mocha.timeout(60000);
</script>
<script src="../test/test.js"></script>

// Load Benchmark as a module.
require(['benchmark'], function(Benchmark) {
benchmarkModule = window.Benchmark = Benchmark;
require(['test'], function() {
QUnit.start();
});
});
}());
<script>
mocha.run();
</script>
</body>

// Set a more readable browser name.
window.onload = function() {
var timeoutId = setInterval(function() {
var ua = document.getElementById('qunit-userAgent');
if (ua && (benchmarkModule || !window.require)) {
ua.innerHTML = platform;
clearInterval(timeoutId);
}
}, 16);
};
</script>
</body>
</html>
</html>
17 changes: 3 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ var root = (typeof global == 'object' && global) || this;
var amd = root.define && define.amd,
slice = Array.prototype.slice;

var assert = require('assert');
var Benchmark = require('../benchmark');
var describe = root.mocha && root.Mocha.describe || require('mocha').describe;
var assert = root.chai && root.chai.assert || require('chai').assert;
var Benchmark = root.Benchmark || require('..');

/** Used to create dummy benchmarks for comparisons. */
function generateBenchData() {
Expand Down Expand Up @@ -46,18 +47,6 @@ describe('Benchmark', function () {
this.skip();
}
});

it('should support loading Platform.js as a module', function () {
if (amd) {
var platform = (benchmarkModule || {}).platform || {},
name = platform.name;

assert.ok(typeof name == 'string' || name === null);
}
else {
this.skip();
}
});
});

/*--------------------------------------------------------------------------*/
Expand Down
13 changes: 0 additions & 13 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ declare class Benchmark {
static reduce<T, K>(arr: T[], callback: (accumulator: K, value: T) => K, thisArg?: any): K;

static options: Benchmark.Options;
static platform: Benchmark.Platform;
static support: Benchmark.Support;
static version: string;

Expand Down Expand Up @@ -86,18 +85,6 @@ declare namespace Benchmark {
queued?: boolean | undefined;
}

export interface Platform {
description: string;
layout: string;
product: string;
name: string;
manufacturer: string;
os: string;
prerelease: string;
version: string;
toString(): string;
}

export interface Support {
browser: boolean;
canInjectScript: boolean;
Expand Down

0 comments on commit 6f51403

Please sign in to comment.