question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Math.xxx >50x slower in userscripts

See original GitHub issue

Issue

calling Math.abs, Math.random, etc. is >50x slower in userscripts

test

to test just copy the following into a userscript open the site it is injected into, and paste the same into the console:

let acc = 0;
console.time("Math.randomx1000000");
for(let i=0; i<1000000; i++) acc += Math.random();
console.timeEnd("Math.randomx1000000");
console.time("Math.absx1000000");
for(let i=0; i<1000000; i++) acc += Math.abs(acc);
console.timeEnd("Math.absx1000000");

// result in userscript:
//   Math.randomx1000000: 4118.552978515625 ms
//   Math.absx1000000: 4082.463134765625 ms
// result in console:
//   Math.randomx1000000: 135.682861328125 ms
//   Math.absx1000000: 55.900634765625 ms

temporary fix

rebinding the object like this fixes the problem:

const Math = window.Math;

despite the simple fix, this caused me major issues (freezes when doing image manipulation) and headaches (took me way to long to debug, because the profiler just shows 40s spent with Math.abs being the only thing on that line)

speculation on the origin

I assume the property access somehow has overhead because of sand-boxing or something, but the Math object is indistinguishable from the normal one (no proxy, no funky getters / setters), so I have no clue what is going on.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
tophfcommented, Jul 20, 2022

I’ve implemented both tricks in #1532.

As for adaptive caching, I doubt it’s trivial so I’m afraid it’ll unnecessarily complicate code.

0reactions
RubenArtmanncommented, Jul 20, 2022

Just an Idea: you could count how many times each property gets used in the windowProxy and then dynamicly enable option 2 for everything where the overhead would be greater than 100ms on the next run.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Untitled
Gr 5 math problems, Sawan aaya hai song photo, Impressive resume samples for ... Basic skills english level 1, Atlantica internacional 2014, Slow...
Read more >
OyM - River Thames Conditions
Correias gates agricolas, Koehring 1466 for sale, Top slow dance songs 70's, ... White picture frame 50 x 40, 6 month olds temperature,...
Read more >
Psychtoolbox News
The new math fixes this, to deal with Retina displays better. Maybe restore backwards compatibility of Psychtoolbox 3.0.18 with macOS versions ...
Read more >
Computer performance evaluation users group (CPEUG)
Applied Mathematics — Electronics and Electrical Engineering^ — Mechanical ... XXX BAD REPLACE - WRONG RECORD ... times as slow as the equivalent...
Read more >
Advanced Bash-Scripting Guide
This is also the division arithmetic operator . ` command substitution The `command` construct makes available the output of command for assignment to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found