Skip to content

Conversation

@budarin
Copy link

@budarin budarin commented Nov 20, 2022

Replace suboptimal code with efficient one

@budarin budarin requested a review from a team as a code owner November 20, 2022 21:16
const toComparators = (range, options) =>
new Range(range, options).set
.map(comp => comp.map(c => c.value).join(' ').trim().split(' '))
.map(comp => comp.map(c => c.value.trim()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't exactly the same thing - [' a ', ' b '] would have previously produced ['a ', ' b'] but will now produce ['a', 'b'].

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all tests are passed


// normalize spaces
range = range.split(/\s+/).join(' ')
range = range.replace(/ +/g, ' ')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change means it will no longer select non-space whitespace characters, including tabs and other unicode whitespace chars.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should provide tests to protect it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, there should be tests, but few projects are willing to change behavior just because there’s not a test yet.

@budarin
Copy link
Author

budarin commented Nov 23, 2022

There are several similar fragments in the code - they basically solve the task assigned to them, but from the point of view of performance, this is just the worst option - we temporarily create 2 arrays and 2 rows and then throw out their garbage.

For execution in the browser - this is a window when it comes to working in a server environment where thousands of requests are executed in parallel - a useless load is created for the garbage collector, which negatively affects the performance of the application

Copy link
Contributor

@lukekarrys lukekarrys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@budarin can you provide benchmarks showing the performance difference of these changes?

@budarin
Copy link
Author

budarin commented Nov 28, 2022

There are no benchmarks, but according to the experience of developing services at high loads, the generation of thousands of requests for such garbage turns into jumps in performance - first a pile of such garbage grows, then the process is suspended by the garbage collector and then everything repeats again

Here, even benchmarks are not needed to understand that extra garbage (generating multiple arrays and strings) just to get another string from the source string will lead to memory costs and, accordingly, increase the time for garbage collection

@budarin
Copy link
Author

budarin commented Jan 10, 2023

Consider storing versions internally not in a string representation

https://marvinh.dev/blog/speeding-up-javascript-ecosystem/
please look at the paragraph "The curious case of semver"

@wraithgar
Copy link
Member

Closing in favor of the discussion/improvements in #528

@wraithgar wraithgar closed this Apr 4, 2023
@budarin budarin deleted the fix branch May 14, 2023 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants