D
Deno

help

Speed of generators

NNeTT7/24/2023
How fast are JS generators when used as iterators? To be clear, I see the for..of loop being very slow compared to a plain for or while loop when used on arrays. Is it because the for..of loop is inherently slow or because the iterator behind it is slow? I'm using a generator for iterating through rows and columns in a "matrix". I'm afraid they'd be too slow for proper use.
Mmarvinh.7/24/2023
I'd just measure the differences in your particular use case given the data size you'll be expecting. My experience is that generators are a bit slower in microbenchmarks, but it really only starts to matter in the real world when the iteration count is high enough that it shows up in measurements
NNeTT7/24/2023
Ay I'll try to do that
Hhexagon56k7/27/2023
Late answer, but an interesting topic <:cookie_deno:1002977285734932480> I agree with marvinh, you should profile your code to see if the actual loop are the bottleneck. If so, is it possible to reduce iteration count? Is it possible to offload these operations to a (rust or whatever) microservice, or offload to wasm within the application? What if you micro-optimize to reasonable performance, and the load/volume increase later? I've found myself having questions like these a couple of times. It has always resulted in a completely different solution at the end 🥸 If you're just curious, you should microbenhmark the sh*t out of it <:party_deno:1035517691517218847> , something like https://github.com/Hexagon/primer (but with loops)
GitHub
GitHub - Hexagon/primer: Benchmark of prime number calculation on p...
Benchmark of prime number calculation on pure javascript vs. wasm - GitHub - Hexagon/primer: Benchmark of prime number calculation on pure javascript vs. wasm
NNeTT7/28/2023
Okay so I ran a benchmark on my matrix between the iterators and using a for loop with indexing. Matrix is stored as a single, long typed array so accessing rows is way faster than columns.
NNeTT7/28/2023
For 100,000 rows and 1,000 columns
NNeTT7/28/2023
And now I did one for 10,000,000 rows and 100 columns
NNeTT7/28/2023
NNeTT7/28/2023
The generators do not seem to reduce performance by a lot. Going with a 10k x 10x square matrix,
NNeTT7/28/2023
import { Matrix } from "https://deno.land/x/vectorizer@v0.0.12/mod.ts";
const matrix = new Matrix(Uint8Array, [1e4, 1e4]);

Deno.bench({
name: "No Op",
fn() {
null;
},
});
Deno.bench({
name: "Generator (Row)",
fn() {
for (const row of matrix.rows()) {
row;
}
},
});
Deno.bench({
name: "Loop (Row)",
fn() {
for (let i = 0; i < matrix.nRows; i += 1) {
matrix.row(i);
}
},
});
Deno.bench({
name: "Generator (Col)",
fn() {
for (const col of matrix.cols()) {
col;
}
},
});
Deno.bench({
name: "Loop (Col)",
fn() {
for (let i = 0; i < matrix.nCols; i += 1) {
matrix.col(i);
}
},
});
import { Matrix } from "https://deno.land/x/vectorizer@v0.0.12/mod.ts";
const matrix = new Matrix(Uint8Array, [1e4, 1e4]);

Deno.bench({
name: "No Op",
fn() {
null;
},
});
Deno.bench({
name: "Generator (Row)",
fn() {
for (const row of matrix.rows()) {
row;
}
},
});
Deno.bench({
name: "Loop (Row)",
fn() {
for (let i = 0; i < matrix.nRows; i += 1) {
matrix.row(i);
}
},
});
Deno.bench({
name: "Generator (Col)",
fn() {
for (const col of matrix.cols()) {
col;
}
},
});
Deno.bench({
name: "Loop (Col)",
fn() {
for (let i = 0; i < matrix.nCols; i += 1) {
matrix.col(i);
}
},
});

Looking for more? Join the community!

Recommended Posts
Parsing HTML/XMLLately I've been researching how to write a simple HTML/XML -> JSON converter. My syntax isn't complIs it possible to import cjs module from es module, both in extension?cjs is added using `deno_core::extension`'s `js`. esm added using `deno_core::extension`'s `esm` IDeno.serve() doesn't streamhttps://gist.github.com/guest271314/39a3f93346bbc7dec5ee9fa1ff579256Deno ImportsI'm trying to use react-hook-form in an application, but bringing in the dependency results in a depWhere to store this key — and how?```ts const cryptoKey = await crypto.subtle.generateKey({ name: 'HMAC', hash: 'SHA-512' }, tTyping DateTimes```ts interface ICity { slug: string name: string description?: string createdAt: Date updBundle without exportIs it possible to have the output not having `export... ` line?LSP badly resolves relative imports between cached modulesI don't really understand this, but it looks to me like the LSP / VS Code has started resolving relaReason for "Following modules were not evaluated"What is the reason for "Following modules were not evaluated; make sure they are imported from otherdiscordav.deno.dev not working nowIm using discordav.deno.dev for get the discord profile picture from id. It was working yesterday buKV: What to return from here?```TS // Create City router.post('/cities', async (context) => { const data = await context.requLog Deno.command process outputi need to execute a command wich will keep running until stopped and log the output. i found this buUncaught TypeError: Cannot set properties of null (setting 'innerHTML')Code: var bcb = new WebSocket('wss://stream.binance.com:9443/ws/bnbusdt@trade'); bcb.onmessage = (It's hard to read that the font used in the official Deno website.On the official Deno website (deno.land), I think it difficult to distinguish between upper and lowePetition to free the module x/type from deno.landHere is the module url: https://deno.land/x/type It says it would be deleted after 30 days but has Docs on understanding how Fresh works under the hood.I want to start becoming a contributor for Fresh, I have been looking at PRs and reading documents wNode MongoDB Driver on Deno dropping connection and crashing processHeya folks. I am encountering the most recent error described in this thread https://github.com/denoHow to solve "cannot be invoked without 'new'" thrown from an NPM module ?Hello, An NPM module (which I don't own) uses `call` on something, which throws a `cannot be invokeCoverage with subprocessOne of my tests involves spawning a subprocess. Is there any way to get this subprocess to participaHow to exclude folder from deno lintI use WebStorm. How to exclude specific folder from deno lint? Is it possible in WebStorm? It is pos