D
Deno

help

Question about for loops

Ppronoob8/17/2023
Why does the following code log numbers from 0 to 9 instead of 1 to 9? i is assigned as zero in the first expression, then it checks whether i is less than 10 and moves to the next expression which is i++, so it should increment the value of i to 1 instead and log it
for (let i = 0; i < 10; i++) {
console.log(i);
}
for (let i = 0; i < 10; i++) {
console.log(i);
}
Ssean8/17/2023
Think about it step by step. You have a variable i with a value of 0 You check if i < 10. If it is, you log the current value. If it isn't, you exit the loop. You increment i.
Ppronoob8/17/2023
I see, so the increment happens at the end or the next iteration
Ssean8/17/2023
Yup it's the last step.
Ppronoob8/17/2023
Thank you so much! ❤️
Ssean8/17/2023
No problem, have a good one :)
Ppronoob8/17/2023
I was so confused
Ssean8/17/2023
Haha I get that
Aabi8/17/2023
It’s good to know how these loops work, but you should rarely (if ever) use them in your actual code 👍
ABAltair 680b8/18/2023
Why not? The good ol for loop is the fastest and most effective way to iterate!
TTheYuriG8/18/2023
exactly, for loop is the fastest way to perform repeated tasks
ABAltair 680b8/18/2023
or iterate over objects or arrays!
NNeTT8/18/2023
I found while loops to be slightly faster than for loops tho Tested back in 2020 NodeJS so I ain't sure
ABAltair 680b8/18/2023
Yes that's still true, The while only tests a Boolean, and the for-loop needs a test and an increment.
TTheYuriG8/18/2023
that would make sense since those don't perform increments and neither instantiates a variable
ABAltair 680b8/18/2023
But if in the while-loop you need an index to do iteration, then it will be the same as for-loop.
Aabi8/18/2023
Heheh I just meant the indexed for-loop, the for-of is fine of course
NNeTT8/18/2023
That was the case I tested When I went beyond 1M iterations While loop showed some difference for..of performs really bad for larger arrays tho.
Aabi8/18/2023
really? didn't know that. does the indexed for-loop perform better? <:pepe_think:946419068024528936>
NNeTT8/18/2023
Way better for..of is fine in places it's required or when you don't have a lot of elements to iterate But it's hella slow as the size increases
Aabi8/18/2023
i hear you! but honestly, personally, unless it's absolutely crucial, i'd still avoid the indexed for-loop

Looking for more? Join the community!

Recommended Posts
Dockerfile saaskit deploymentI created an entry in `deno.json` starting a script named `db:init` to initialize my KV DB with an uDeno TypesWhat is the Deno.ServeHandler type? ```No overload matches this call. Overload 1 of 3, '(handler: I use Deno KV for my application, and I'm planning to use Deno Deploy. Can I allow deploy to use...I use Deno KV for my application, and I'm planning to use Deno Deploy. Can I allow deploy to use KVbad resource id with node:crypto```ts function test() { const key = randomBytes(32); const iv = randomBytes(16); const cipher Close a Deno.serve() connection?Is it possible to close a Deno.serve() connection?Relative import path "preact" not prefixed with / or ./ or ../ at https://deno.land/x/fresh@1.4.Please help. ``` deno 1.36.1 (release, aarch64-apple-darwin) v8 11.6.189.12 typescript 5.1.6 ```Problems with Deno KV```ts import InputLoop from "https://deno.land/x/input@2.0.3/index.ts"; const kv = await Deno.openKFile differs only by case errorI'm getting a strange error from TypeScript. For some reason it thinks the case of my file name is Possible to have multiple http servers listening to different ports in the same module?I'm porting a service mesh platform we wrote for NodeJS over to Deno and it is based on the idea of How to have private state in embedded deno_core JsRuntime?I need to either keep a reference in rust to an object I then `delete globalThis.__context`, but I cIssues getting local environment running in VSCode and Visual Studio 2022I'm a windows user trying to contribute on an github issue, but I've run into a few blockers. Using Relative import path "ioredis" not prefixed with / or ./ or ../```ts // main.ts import Redis from "ioredis"; ``` ```json // deno.json { "imports": { "ioredisPixi.js breaks DenoI'm trying to import Pixi in a Fresh island: ``` import { Sprite, Stage } from "npm:@pixi/react@7.1Confused by deno vendor-related behaviorsI created a main.ts containing this code: ``` import * as thrift from "npm:thrift"; let connection =How to release node:zlib resourcesI have some testcases which use the `node:zlib` polyfill and I'm having a bit of trouble getting theDeno Repl ProgrammaticallyPython has a really nice not-well-known tool for creating a repl at any point in the code (kinda likDeno for SASS dependencies?I'm trying to understand how I would migrate from Jekyll and NPM to Lume and Deno. Specifically, I uHow can I send a number across a Deno.TcpConn as stream of bytes?I need to send a number across a Deno.TcpConn as 4 bytes, so it can be read by a server as an int32.Migrating `Deno.run` to `Deno.command` with `readlines`I had the following: ```ts browserProcess = Deno.run({ cmd: buildArgs, // eg chrome exe path, thError: Not implemented: crypto.KeyObject.prototype.asymmetricKeyTypeHello. Im trying to create a RSA Keypair, using the node **crypto.generateKeyPair** function, but i