-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
994c44b
commit 9b7b69c
Showing
3 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |