Skip to content

Instantly share code, notes, and snippets.

View MadhuNimmo's full-sized avatar
🎯
Focusing

Madhurima Chakraborty MadhuNimmo

🎯
Focusing
View GitHub Profile
@MadhuNimmo
MadhuNimmo / race-condition.js
Created December 8, 2022 23:55 — forked from ubershmekel/race-condition.js
An example race condition in JavaScript
// An example race condition in JavaScript
// When you run this script using Node or in a browser, you'll find it
// does not print "Ended with 0", but a random number. Even though the functions running
// simply loop 100 iterations of adding and subtracting. The reason the end result is random
// is because the sleeps are of random duration and the time between the read of the variable
// causes the eventual write to be incorrect when `adder` and `subber` interleave.
// This problem is similar to https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use
let number = 0;
const times = 100;
@MadhuNimmo
MadhuNimmo / rangeerror.js
Created October 14, 2022 19:53
Simplest solution to JSON.stringify RangeError: Invalid string length
var out="[";
for(var indx=0;indx<data.length-1;indx++){
out+=JSON.stringify(data[indx],null,4)+",";
}
out+=JSON.stringify(data[data.length-1],null,4)+"]";