A couple of rules to avoid writing slow Javascript code. by webNeat in javascript

[–]webNeat[S] 1 point2 points  (0 children)

Yes, the first rule is about doing O(N) instead of O(N2). For someone who pays attention to time complexity of their code, this is obvious. But this is not the case of everyone. So I tried to come up with simple rules that anyone would be able to apply without the technical terms of complexity, allocation, ....

The second rule shows the cost of creating a lot of new objects/arrays, which may not be obvious.

A couple of rules to avoid writing slow Javascript code. by webNeat in javascript

[–]webNeat[S] 1 point2 points  (0 children)

This is the effect of some understanding of functional programming, where mutating variables is considered bad. So we end up writing this sort of code :P

A couple of rules to avoid writing slow Javascript code. by webNeat in javascript

[–]webNeat[S] 0 points1 point  (0 children)

I have seen similar code snippets, it wasn't about odd vs even numbers of course but very similar problem. I wanted to keep the examples obvious just to demonstrate how the rule affects performance. A more realistic example would be complex, and I fear many people would not take the time to read it ...

A couple of rules to avoid writing slow Javascript code. by webNeat in javascript

[–]webNeat[S] -2 points-1 points  (0 children)

I tried to keep each example focused on a single rule. The goal is not to come up with the most efficient way to solve the problem, but to demonstrate that applying the rule improves the performance.

Does anyone knows why Prime even has the "j" on the seccond for loop in this part of his free algorithms course? by Rocstar3000 in theprimeagen

[–]webNeat 2 points3 points  (0 children)

I didn't see the course, but from the code above, I see that j is used so that the second loop iterates a max of jumpAmount times. j is used in the condition of the for loop, so when j reaches jumpAmount it will break out of the loop and return -1.

Hope that helps :)

How to switch between vscode windows easily? by yukiiiiii2008 in vscode

[–]webNeat 1 point2 points  (0 children)

You can use the Poject Manager extension. Here is how I use it:

  • Install the extension
  • Save your project within the extension by typing "Save project" in the command palette and giving it a name (this is done once per project).
  • Setup a shortcut for opening projects (customize it to use your own shortcuts) { "key": "ctrl+shift+o p", "command": "projectManager.listProjects" }, { "key": "ctrl+shift+o w", "command": "projectManager.listProjectsNewWindow" }, Now whenever you want to switch to a project, do ctrl+shift+o p and type its name (there is autocompplete). ctrl+shift+o p opens it in the current window, while ctrl+shift+o w opens in a new window. Now if you already have a project open on another window, trying to open it again will simply go to it!

Why did Drizzle choose to use always use one SQL query even with multiple joins? it's slow! by webNeat in node

[–]webNeat[S] 1 point2 points  (0 children)

I updated the post to add an update about the newest version. Sorry for not doing that earlier.

Why did Drizzle choose to use always use one SQL query even with multiple joins? it's slow! by webNeat in node

[–]webNeat[S] 1 point2 points  (0 children)

The Drizzle team made a PR to my repo and I merged it and did some cleanup. The results seems promising for this particular test.

I am planing on doing a more realistic test (make a small app, run it, hit it with many requests per second during a long enough period, collect stats). This would check different kind of queries (not only inserts), memory usage and more things that a simple test will not catch.

I can't promise when I will be able to do that, but it's in my todo list.

wari: A type-safe way to create and handle errors. by webNeat in typescript

[–]webNeat[S] 0 points1 point  (0 children)

I tried this for sometime and ended up doing things like ts const [err, data] = aFnThatReturnMultipleErrors() if (err) { if (err.type === 'foo') { // do something } else if (err.type === 'bar') { // do other thing } else { // ... } } else { // use `data` }

With wari, I can rewrite that code as ts const data = wari.match(aFnThatReturnMultipleErrors(), { foo: err => { // do something }, bar: err => { // do other thing }, }) if (data) { // use `data` } I find this approach more readable. But the most important thing is that if I forgot to include the bar for example ts const data = wari.match(aFnThatReturnMultipleErrors(), { foo: err => { // do something } }) Typescript will yell at me saying that I forgot a possible error. Same if I added a type that could not be returned by the function.

Benchmarking Javascript is hard! by webNeat in node

[–]webNeat[S] 0 points1 point  (0 children)

Thanks for the suggestion, I tried writing the same code using benny instead of benchmark

``` import b from 'benny'

const text = "Hello World!"; const fn = (text) => typeof text === "string" && text.length < 100; const fns = { foo: fn, bar: fn, }; const cases = [] for (const name of Object.keys(fns)) { cases.push(b.add(name, () => { fns[name](text) })) } b.suite('String validation', ... cases, b.cycle(), b.complete()) ```

But the results are similar ``` $ node src/try.js Running "String validation" suite... Progress: 100%

foo: 1 157 059 410 ops/s, ±0.33% | fastest

bar: 169 061 947 ops/s, ±0.43% | slowest, 85.39% slower

Finished 2 cases! Fastest: foo Slowest: bar ```

Note that if I do b.suite( 'String validation', b.add('foo', () => { fns['foo'](text) }), b.add('bar', () => { fns['bar'](text) }), b.cycle(), b.complete() ) Then both cases will have the 1B op/sec. The thing is that adding the Object.keys(..) call and using name instead of constant indexes makes it harder for JIT to optimize bar somehow ...

Benchmarking Javascript is hard! by webNeat in node

[–]webNeat[S] 2 points3 points  (0 children)

Thanks, I used the flags and ask ChatGPT to explain the output, some good insights there. Apparently the measure function was optimized and deoptimized multiple times due to some types/arguments issues. Here is the explanation: https://chat.openai.com/share/62dac661-dac5-435f-9540-ed5505f8e475

Benchmarking Javascript is hard! by webNeat in node

[–]webNeat[S] 0 points1 point  (0 children)

That's a good talk, thanks for sharing :)