-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(convertPaths): clean up plugin #1913
Conversation
83ec4ef
to
32c0720
Compare
Did the testing really show that? I would think a function that does it all in native code must be faster than going back and forth between JS and native code. Edit: According to https://github.com/bmeurer/js-micro-benchmarks/blob/60a34c0dd29b77e6950555c2dd9687b1a0a7671e/bench-math-hypot.js |
Yes and no! There is a measurable difference in benchmarks, but in practice the difference isn't really significant and may be margin of error (< 1 second). I opted to run with it since I took the time to check it out anyway. Here are some benchmarks I ran: const benchmark = require('benchmark');
const suite = new benchmark.Suite;
const data = [
[3, 4],
[10, 10],
[134, 5235],
[352523, 52352],
[525.52352, 532625.5235235],
[4245, 1352.2352],
];
suite
.add('Math.sqrt', () => data.map(([dx, dy]) => Math.sqrt(dx ** 2 + dy ** 2)))
.add('Math.hypot', () => data.map(([dx, dy]) => Math.hypot(dx, dy)))
.add('**', () => data.map(([dx, dy]) => (dx ** 2 + dy ** 2) ** 0.5))
.on('cycle', (event) => console.log(String(event.target)))
.run({ 'async': true });
// Math.sqrt x 5,724,619 ops/sec ±1.87% (89 runs sampled)
// Math.hypot x 3,313,527 ops/sec ±0.74% (95 runs sampled)
// ** x 5,466,879 ops/sec ±0.96% (95 runs sampled) When looking into why
In other words, |
I'd still use BTW, I recall I had attempted to apply some fixes (similar to the ones you make) across the codebase a couple of years ago which I found with xo, but we never landed them. |
This is different, actually. I assumed the bitwise operator was used for performance, so I did actually check before replacing that with A faster approach would be to use a const benchmark = require('benchmark');
const suite = new benchmark.Suite;
const data = [
'c',
's',
'a',
'C',
'S',
'A'
];
const str = 'cs';
const set = new Set([
'c',
's'
]);
suite
.add('Bitwise indexOf', () => data.map((d) => ~str.indexOf(d) ? 1 : -1))
.add('String includes', () => data.map((d) => str.includes(d) ? 1 : -1))
.add('Set has', () => data.map((d) => set.has(d) ? 1 : -1))
.add('Eq', () => data.map((d) => d === 'c' || d === 's' ? 1 : -1))
.on('cycle', (event) => console.log(String(event.target)))
.run({ 'async': true });
// Bitwise indexOf x 18,353,976 ops/sec ±2.19% (91 runs sampled)
// String includes x 18,999,212 ops/sec ±1.33% (94 runs sampled)
// Set has x 24,346,799 ops/sec ±1.79% (96 runs sampled)
// Eq x 59,498,845 ops/sec ±1.91% (93 runs sampled)
I agree with this in general. The reason I opted to change it was just because I felt both were clean enough tbh. However, given the difference is negligible, when I'm done with what I'm working on, I'll just double-check if it made a practical difference in our regression tests, and if not, I'll change it back. 👍🏽 Btw, the reason I'm nitpicking on performance so much is for this. I'm usually using a profiler, but the ones here were just me combing through while refactoring: I'd like to get this down as low as possible, ideally below 10 minutes, then merge it into the repo. |
Don't get me wrong, I'm all for performance myself. I just don't think it's worth reducing readability/maintainability unless we hit a critical path and performance is noticeable. but I think we are all on the same boat here :) |
While reviewing #1889 I'd made some changes locally which I just wanted to push.
This has no (or negligible) impact on performance, it's just to keep things cleaner.
any
onprev
. We were literally disabling type checking with magic comments on every usage anyway.string.includes
instead of~string.indexOf
as there is no significant difference while the former is more semantic.apply
methods, we can just use the spread operator.Math.sqrt
instead ofMath.hypot
asMath.hypot
has overhead from improved precision, which we don't need here.5e-324
(float) and1.7976931348623157e+308
(integer)