diff options
author | 2021-09-21 00:58:55 -0700 | |
---|---|---|
committer | 2021-09-21 00:58:55 -0700 | |
commit | 4e12a4c0130aa652388db3d1c2cbfeca3f19b1e2 (patch) | |
tree | bd9a37688ca85e1ebee1b27d8cd7a48246083153 | |
parent | e869cdca9f89468dd9c0b6f166fc43b79d665318 (diff) | |
download | bun-4e12a4c0130aa652388db3d1c2cbfeca3f19b1e2.tar.gz bun-4e12a4c0130aa652388db3d1c2cbfeca3f19b1e2.tar.zst bun-4e12a4c0130aa652388db3d1c2cbfeca3f19b1e2.zip |
[Bun.js] Add support for `console.time`, `console.timeEnd`
-rw-r--r-- | src/javascript/jsc/bindings/exports.zig | 39 | ||||
-rw-r--r-- | src/javascript/jsc/javascript.zig | 1 |
2 files changed, 38 insertions, 2 deletions
diff --git a/src/javascript/jsc/bindings/exports.zig b/src/javascript/jsc/bindings/exports.zig index 3babc0dda..b2e556e04 100644 --- a/src/javascript/jsc/bindings/exports.zig +++ b/src/javascript/jsc/bindings/exports.zig @@ -816,9 +816,44 @@ pub const ZigConsoleClient = struct { pub fn count(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void {} pub fn countReset(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void {} - pub fn time(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void {} + + const PendingTimers = std.AutoHashMap(u64, ?std.time.Timer); + threadlocal var pending_time_logs: PendingTimers = undefined; + threadlocal var pending_time_logs_loaded = false; + + pub fn time(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void { + const id = std.hash.Wyhash.hash(0, chars[0..len]); + if (!pending_time_logs_loaded) { + pending_time_logs = PendingTimers.init(default_allocator); + pending_time_logs_loaded = true; + } + + var result = pending_time_logs.getOrPut(id) catch unreachable; + + if (!result.found_existing or (result.found_existing and result.value_ptr.* == null)) { + result.value_ptr.* = std.time.Timer.start() catch unreachable; + } + } + pub fn timeEnd(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void { + if (!pending_time_logs_loaded) { + return; + } + + const id = std.hash.Wyhash.hash(0, chars[0..len]); + var result = (pending_time_logs.fetchPut(id, null) catch null) orelse return; + const value: std.time.Timer = result.value orelse return; + // get the duration in microseconds + // then display it in milliseconds + Output.printElapsed(@intToFloat(f64, value.read() / std.time.ns_per_us) / std.time.us_per_ms); + switch (len) { + 0 => Output.printErrorln("\n", .{}), + else => Output.printErrorln(" {s}", .{chars[0..len]}), + } + + Output.flush(); + } + pub fn timeLog(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize, args: *ScriptArguments) callconv(.C) void {} - pub fn timeEnd(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void {} pub fn profile(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void {} pub fn profileEnd(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void {} pub fn takeHeapSnapshot(console: ZigConsoleClient.Type, global: *JSGlobalObject, chars: [*]const u8, len: usize) callconv(.C) void {} diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index bd9b470a9..e4ccd62e5 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -181,6 +181,7 @@ pub const Bun = struct { return ref; } + pub fn readFileAsString( this: void, ctx: js.JSContextRef, |