TGViewer
Channel Public Channel
JavaScript

JavaScript

@javascript

A resourceful newsletter featuring the latest and most important news, articles, books and updates in the world of #javascript 🚀 Don't miss our Quizzes!

Let's chat: @nairihar
Subscribers
31.1K
Photos
1.2K
Videos
10
Links
895

Showing posts older than #2597 · Back to latest

Older Posts 20 shown
Post #2595 4.2K
  • ❤ 2
  • 👍 2
  • 🔥 1
Post #2594 4.14K
CHALLENGE

const arr = new Array(1000000).fill(0).map((_, i) => i);
const results = [];

function method1() {
return arr.filter(x => x % 2 === 0).map(x => x * 2).slice(0, 3);
}

function method2() {
const result = [];
for (let i = 0; i < arr.length && result.length < 3; i++) {
if (arr[i] % 2 === 0) {
result.push(arr[i] * 2);
}
}
return result;
}

console.log(method1().join(','));
console.log(method2().join(','));
Post #2592 4.03K
  • ❤ 8
  • 🤔 5
  • 👍 3
Post #2591 4.04K
CHALLENGE

async function fetchData() {
console.log('1');
return Promise.resolve('data');
}

async function processData() {
console.log('2');
const result = await fetchData();
console.log('3');
return result;
}

console.log('4');
processData().then(() => console.log('5'));
console.log('6');
Post #2589 4.16K
  • ❤ 5
  • 👍 3
Post #2588 4.04K
CHALLENGE

const original = { a: 1, b: { c: 2 } };
const shallow = { ...original };
const deep = JSON.parse(JSON.stringify(original));

shallow.a = 10;
shallow.b.c = 20;

deep.a = 100;
deep.b.c = 200;

const frozen = Object.freeze({ x: 1, y: { z: 2 } });
frozen.x = 99;
frozen.y.z = 99;

console.log(original.a, original.b.c, frozen.x, frozen.y.z);
  • ❤ 3
  • 🔥 2
Post #2587 3.98K
🤟 How We Migrated Our Rush.js Monorepo to Node Type Stripping

Since v23.6 (and in LTS since v22.18.0), Node has supported running (most) TypeScript code by stripping the types out first. The Calm team was excited about the potential for improving productivity and DX, and set a migration in process. Here’s a look at the challenges encountered and what the end results were.

Stuart Dotson (Calm)
  • 🔥 7
  • ❤ 2
  • 👍 1
Post #2586 3.77K
  • ❤ 8
  • 👍 2
  • 🔥 1
Post #2585 4.04K
CHALLENGE

class EventBus {
constructor() {
this.events = new Map();
}

on(event, callback) {
if (!this.events.has(event)) {
this.events.set(event, []);
}
this.events.get(event).push(callback);
}

emit(event, data) {
if (this.events.has(event)) {
this.events.get(event).forEach(callback => callback(data));
}
}
}

const bus = new EventBus();
bus.on('user', name => console.log(`Hello ${name}`));
bus.on('user', name => console.log(`Welcome ${name}`));
bus.emit('user', 'Sarah');
bus.emit('admin', 'John');
  • ❤ 6
  • 🤔 3
  • 👍 1
  • 🤣 1
Post #2584 3.84K
Is there a difference between the previous code (red) and the new one (green) ?

Actually, there is 35second difference 😠

The previous code was, on every loop, creating a new object, always deallocating and reallocating memory, while the second example creates one object (a cache) and save each item into it.

Eduard Krivánek
  • 👍 13
  • 🔥 4
  • ❤ 3
Post #2583 3.88K
  • 🔥 5
  • 🤔 2
Post #2582 4K
CHALLENGE

const promise1 = Promise.resolve('first');
const promise2 = new Promise(resolve => {
resolve('second');
});

const promise3 = Promise.resolve().then(() => 'third');

async function test() {
console.log('start');

const result1 = await promise1;
console.log(result1);

const result2 = await promise2;
console.log(result2);

const result3 = await promise3;
console.log(result3);

console.log('end');
}

test();
  • ❤ 3
Post #2581 3.95K
  • 👍 5
  • ❤ 3
  • 🔥 2
Post #2580 4.13K
CHALLENGE

function createCounter() {
let count = 0;
return {
increment: () => ++count,
decrement: () => --count,
get: () => count
};
}

const counter1 = createCounter();
const counter2 = createCounter();
counter1.increment();
counter1.increment();
counter2.increment();
console.log(counter1.get(), counter2.get());
const { increment, get } = counter1;
increment();
console.log(get());
  • ❤ 1
Post #2579 3.97K
If Linus Torvalds applied for a junior coding job today…
He’d get rejected.

He doesn’t have “5 years React experience.”

That’s how broken hiring has become. 😨
  • 🤣 18
  • 🔥 14
  • ❤ 3
  • 🤩 1
Post #2578 3.99K
  • ❤ 2
  • 👍 2
  • 🔥 2
Post #2577 4.06K
CHALLENGE

let obj1 = { name: 'Sarah' };
let obj2 = { name: 'Mike' };
let obj3 = { ref: obj1 };

obj1.circular = obj1;
obj2.partner = obj3;
obj3.partner = obj2;

let weakMap = new WeakMap();
weakMap.set(obj1, 'data1');
weakMap.set(obj2, 'data2');

obj1 = null;
obj2 = null;

console.log(weakMap.has(obj3.ref));
console.log(obj3.partner.name);
  • ❤ 5
Older posts →
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →