Skip to content

Commit

Permalink
Fri May 24 12:09:29 EEST 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
costinEEST committed May 24, 2024
1 parent 994c44b commit 9b7b69c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

# Chapters

- [Chapter 02: Block scoped declarations](./src/02-block-scoped-declarations)
- [Chapter 04: Classes](./src/04-classes)
- [Chapter 05: New Object features](./src/05-new-object-features)
- [Chapter 05: New `Object` features](./src/05-new-object-features)
- [Chapter 11: New `Array` features](./src/11-new-array-features)
21 changes: 21 additions & 0 deletions src/02-block-scoped-declarations/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
```js
for (let i = 0; i < 5; i++) {
setTimeout(() => console.log(i), i * 1000);
}
```

or

```js
for (var i = 0; i < 5; i++) {
setTimeout((capturedI) => console.log(capturedI), i * 1000, i);
}
```

or

```js
for (var i = 0; i < 5; i++) {
((capturedI) => setTimeout(() => console.log(capturedI), i * 1000))(i);
}
```
54 changes: 53 additions & 1 deletion src/11-new-array-features/readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,53 @@
- https://www.freecodecamp.org/news/how-to-create-a-javascript-utility-library-like-lodash
- https://www.freecodecamp.org/news/how-to-create-a-javascript-utility-library-like-lodash
- [Convert from `Base64` to `byte array`](https://stackoverflow.com/questions/21797299/convert-base64-string-to-arraybuffer):

```js
function base64ToArrayBuffer(base64) {
// Solution 1
const binaryString = atob(base64);
const bytes = new Uint8Array(binaryString.length);

for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}

return bytes.buffer;
}

function base64ToArrayBuffer2(base64) {
// Solution 2
return Uint8Array.from(atob(base64), (c) => c.charCodeAt(0)).buffer;
}

// Benchmark function
function benchmark(func, base64, iterations) {
const start = performance.now();

for (let i = 0; i < iterations; i++) {
func(base64);
}

const end = performance.now();

return end - start;
}

const base64Sample = "SGVsbG8gd29ybGQ="; // "Hello world" in base64
const iterations = 100000;

const time1 = benchmark(base64ToArrayBuffer, base64Sample, iterations);
const time2 = benchmark(base64ToArrayBuffer2, base64Sample, iterations);

console.log(`Solution 1 took: ${time1} ms`);
console.log(`Solution 2 took: ${time2} ms`);

if (time1 < time2) {
console.log("Solution 1 is faster.");
} else if (time1 > time2) {
console.log("Solution 2 is faster.");
} else {
console.log("Both solutions are roughly the same speed.");
}

// Solution 1 is faster
```

0 comments on commit 9b7b69c

Please sign in to comment.