diff options
author | Dmitry Sharshakov <sh7dm@outlook.com> | 2019-02-02 09:27:42 +0300 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-02-02 01:27:42 -0500 |
commit | 16ed1f2545b34851ebdb9095251236fb51e39f5d (patch) | |
tree | ef88386129adefaf0ed22f150dff01b1f134563e /js | |
parent | e5899b14e268b6b636b04b8f5a78df4c23e00478 (diff) |
Add performance.now (#1633)
Diffstat (limited to 'js')
-rw-r--r-- | js/globals.ts | 3 | ||||
-rw-r--r-- | js/performance.ts | 29 | ||||
-rw-r--r-- | js/performance_test.ts | 10 | ||||
-rw-r--r-- | js/unit_tests.ts | 1 |
4 files changed, 43 insertions, 0 deletions
diff --git a/js/globals.ts b/js/globals.ts index e45aee9e2..9e7d20bcc 100644 --- a/js/globals.ts +++ b/js/globals.ts @@ -21,6 +21,7 @@ import * as timers from "./timers"; import * as url from "./url"; import * as urlSearchParams from "./url_search_params"; import * as workers from "./workers"; +import * as performanceUtil from "./performance"; // These imports are not exposed and therefore are fine to just import the // symbols required. @@ -93,4 +94,6 @@ export type TextEncoder = textEncoding.TextEncoder; window.TextDecoder = textEncoding.TextDecoder; export type TextDecoder = textEncoding.TextDecoder; +window.performance = new performanceUtil.Performance(); + window.workerMain = workers.workerMain; diff --git a/js/performance.ts b/js/performance.ts new file mode 100644 index 000000000..73378b15c --- /dev/null +++ b/js/performance.ts @@ -0,0 +1,29 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import * as msg from "gen/msg_generated"; +import { sendSync } from "./dispatch"; +import * as flatbuffers from "./flatbuffers"; +import { assert } from "./util"; + +export class Performance { + timeOrigin = 0; + + constructor() { + this.timeOrigin = new Date().getTime(); + } + + /** Returns a current time from Deno's start + * + * const t = performance.now(); + * console.log(`${t} ms since start!`); + */ + now(): number { + const builder = flatbuffers.createBuilder(); + msg.Now.startNow(builder); + const inner = msg.Now.endNow(builder); + const baseRes = sendSync(builder, msg.Any.Now, inner)!; + assert(msg.Any.NowRes === baseRes.innerType()); + const res = new msg.NowRes(); + assert(baseRes.inner(res) != null); + return res.time().toFloat64() - this.timeOrigin; + } +} diff --git a/js/performance_test.ts b/js/performance_test.ts new file mode 100644 index 000000000..154dadbbe --- /dev/null +++ b/js/performance_test.ts @@ -0,0 +1,10 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +import { test, assert } from "./test_util.ts"; + +test(function now() { + const start = performance.now(); + setTimeout(() => { + const end = performance.now(); + assert(end - start >= 10); + }, 10); +}); diff --git a/js/unit_tests.ts b/js/unit_tests.ts index 418779f7f..edad57893 100644 --- a/js/unit_tests.ts +++ b/js/unit_tests.ts @@ -43,6 +43,7 @@ import "./truncate_test.ts"; import "./url_test.ts"; import "./url_search_params_test.ts"; import "./write_file_test.ts"; +import "./performance_test.ts"; import "../tools/util_test.ts"; |