Skip to content

Instantly share code, notes, and snippets.

View sandeep-cs-dev's full-sized avatar

Sandeep Patel sandeep-cs-dev

View GitHub Profile
//index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Socket.io Demo</title>
</head>
<body
<script src="/socket.io/socket.io.js"></script>
@sandeep-cs-dev
sandeep-cs-dev / nextTick.js
Created February 23, 2020 19:48 — forked from mmalecki/nextTick.js
process.nextTick vs setTimeout(fn, 0)
for (var i = 0; i < 1024 * 1024; i++) {
process.nextTick(function () { Math.sqrt(i) } )
}
@sandeep-cs-dev
sandeep-cs-dev / output.md
Created February 23, 2020 22:35 — forked from brycebaril/output.md
process.nextTick vs setImmediate

@mafintosh asks: "Does anyone have a good code example of when to use setImmediate instead of nextTick?"

https://twitter.com/mafintosh/status/624590818125352960

The answer is "generally anywhere outside of core".

process.nextTick is barely asynchronous. Flow-wise it is asynchronous, but it will trigger before any other asynchronous events can (timers, io, etc.) and thus can starve the event loop.

In this script I show a starved event loop where I just synchronously block, use nextTick and setImmediate

async function test () {
let result1 = await asyncCall1()
let result2 = await asyncCall2()
return {result1,result2};
}
/* optimized version
As both opertion are independent so we can run function in Parallel
function asyncAvg(n, avgCB) {
// Save ongoing sum in JS closure.
var sum = 0;
function help(i, cb) {
sum += i;
if (i == n) {
cb(sum);
return;
}
@sandeep-cs-dev
sandeep-cs-dev / sorter.js
Created March 3, 2020 15:24 — forked from liron-navon/sorter.js
Worker threads example
const { parentPort, workerData, isMainThread } = require("worker_threads");
// CPU consuming function (sorting a big array)
function sortBigArray(bigArray) {
return bigArray.sort((a, b) => a - b);
}
// check that the sorter was called as a worker thread
if (!isMainThread) {
// make sure we got an array of data
@sandeep-cs-dev
sandeep-cs-dev / decrypt.js
Created March 12, 2020 14:36 — forked from fratuz610/decrypt.js
Encrypt from Java and decrypt on Node.js - aes 256 ecb
// we determine the key buffer
var stringKey = "example";
var cipherText = ".........";
// we compute the sha256 of the key
var hash = crypto.createHash("sha256");
hash.update(stringKey, "utf8");
var sha256key = hash.digest();
var keyBuffer = new Buffer(sha256key);
var express = require('express'),
request = require('request'),
bodyParser = require('body-parser'),
app = express();
var myLimit = typeof(process.argv[2]) != 'undefined' ? process.argv[2] : '100kb';
console.log('Using limit: ', myLimit);
app.use(bodyParser.json({limit: myLimit}));
function corsProxyTest() {
let targetOrigin = 'http://example.com';
let proxyUrl = 'http://cors-proxy.com/post/user';
let user = {userId:"userId","post":"hello!"};
fetch(proxyUrl, {
@sandeep-cs-dev
sandeep-cs-dev / LearnXInYMinProtocolBuffer.proto
Created February 15, 2021 22:02 — forked from shankarshastri/LearnXInYMinProtocolBuffer.proto
Self-Explanatory Protocol Buffer Lang Guide (CheatSheet)
/*
* Self-Explanatory Protocol Buffer Lang Guide
*/
/*
* Why Protocol Buffers?
* Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.
* You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.
* Protocol Buffers are Schema Of Messages. They are language agnostic.