Skip to content
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/for each to forloops #50

Merged
merged 13 commits into from
Feb 6, 2020

Conversation

JacobMGEvans
Copy link
Contributor

@JacobMGEvans JacobMGEvans commented Feb 2, 2020

  • forEach on methods converted to ES1 for loop - leaving comments for PR review confirmation
    • Once the PR has been reviewed and the logic is confirmed I will remove the commented out forEach logic.
  • forEach converted to for loop -- function ensures middleware is a function
  • let & const to var for potential small perf -- tests passing in both prior commits

should resolve #42
Screen Shot 2020-02-01 at 8 42 15 PM

@JacobMGEvans
Copy link
Contributor Author

Still have more forEach to refactor, thought I would get the ball rolling with PR review, suggestions, ect... 😄

@fedyk
Copy link

fedyk commented Feb 2, 2020

@JacobMGEvans thanks for your PR. Would you be able to create some simple benchmark test to prove that those changes improve boot perf?

@JacobMGEvans
Copy link
Contributor Author

JacobMGEvans commented Feb 2, 2020

@JacobMGEvans thanks for your PR. Would you be able to create some simple benchmark test to prove that those changes improve boot perf?

I'll definitely give it a shot, any suggestions on tooling or the like? Or should I add actual tests that are focusing on execution time to the test suites?

@niftylettuce any ideas or opinions on this?

@JacobMGEvans
Copy link
Contributor Author

The only forEach left are in the testing suites, which converting those seemed unnecessary since they would only affect development environments.

Still unclear on how benchmarking should be done for a clear picture of the potential effects of the refactoring done.

@JacobMGEvans
Copy link
Contributor Author

JacobMGEvans commented Feb 3, 2020

I fixed the benchmark code... Mostly. I wasn't sure what the nest() method was replaced by in the API so I used .middleware() seemed to handle the change ok.

I will create a separate PR with the benchmark code changes. 😃
#51 is created, I am not sure but I think the server.js file can be improved for benchmark accuracy and stability/consistency.

@JacobMGEvans JacobMGEvans force-pushed the refactor/forEach-to-forloops branch from 8249e64 to 02cf1fa Compare February 3, 2020 05:34
@fedyk
Copy link

fedyk commented Feb 3, 2020

@JacobMGEvans there is popular library benchmarkjs for performance tests. But I'm not sure it is suitable for testing boot time(the metric you are trying to improve by this PR).

You can use nodejs build-in profiler from V8 and try to test your changes. Here guide from Node.js docs.

In case of profiling the boot time, you can create file test.js:

const Route = require("./")

And then run NODE_ENV=production node --prof test.js and node --prof-process isolate-0x102808000-v8.log > processed.txt. Basically, in processed.txt you would have a result of profiling. By running it a few times for the master and your branch, you should have the raw result of your changes.

This should be enough for test parsing and compiling times.

@JacobMGEvans
Copy link
Contributor Author

@JacobMGEvans there is popular library benchmarkjs for performance tests. But I'm not sure it is suitable for testing boot time(the metric you are trying to improve by this PR).

You can use nodejs build-in profiler from V8 and try to test your changes. Here guide from Node.js docs.

In case of profiling the boot time, you can create file test.js:

const Route = require("./")

And then run NODE_ENV=production node --prof test.js and node --prof-process isolate-0x102808000-v8.log > processed.txt. Basically, in processed.txt you would have a result of profiling. By running it a few times for the master and your branch, you should have the raw result of your changes.

This should be enough for test parsing and compiling times.

I'll definitely give it a try 😁

@JacobMGEvans
Copy link
Contributor Author

Boot Profiling Files:
RefactoredProcessed.txt
MasterProcessed.txt

I noticed some reduced ticks in the RefactoredProcessed and even some processes completely gone, though I might just be missing them.

@niftylettuce
Copy link
Contributor

No need for benchmarks

@niftylettuce niftylettuce merged commit e07397c into koajs:master Feb 6, 2020
@niftylettuce
Copy link
Contributor

JacobMGEvans added a commit to JacobMGEvans/router that referenced this pull request Mar 14, 2020
* forEach on methods converted to ES1 for loop - leaving comments for PR review confirmation

* forEach converted to forloop -- function ensures middleware is a function

* let & const to var for potential small perf -- tests passing in both commits

* Removed comments -- git can be used to diff the logic

* forEach converted to for loop -- seems to build the middleware for server use

* forEach converted to for loop -- param for loop needed a function closure to not hang up the tests

* setPrefix forEach converted -- all 92 tests passing
- Renamed the previous function to setFUNCTION

* support array of paths -- 92 tests pass

* converted forEach loop that iterates over cloneRouter stack

* converted route matching and method assign nested loop

* converted loop that supports array of paths and registers routes

* converted add parameter middleware loop -- all 92 tests passing

* Last forEach converted -- also removed a unused param from a function
- All 92 tests passing
- Some code cleanup
- Minor formatting mainly to refactored sections
- No benchmarks currently
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.

forEach should be rewritten to for loop for optimized boot performance
3 participants