Skip to content

Instantly share code, notes, and snippets.

View shubham-sharmas's full-sized avatar

Shubham Sharma shubham-sharmas

  • 07:35 (UTC +05:30)
View GitHub Profile
@shubham-sharmas
shubham-sharmas / node-js-worker-threads-example.js
Created November 16, 2023 18:47
This gist demonstrates the node.js worker threads example by simulating a time-consuming task.
const { Worker, isMainThread, parentPort } = require("worker_threads");
if (isMainThread) {
console.log("Main thread started.");
// Create a new worker thread
const worker = new Worker(__filename);
// Listen for messages from the worker thread
worker.on("message", (result) => {
@shubham-sharmas
shubham-sharmas / worker_thread_import.js
Created November 16, 2023 18:42
This gist demonstrates how we can import Node.js worker threads in ES5 using require and ES6 using import statement.
/ ES5
const worker = require('node:worker_threads');
// ES6
import worker from 'node:worker_threads';
@shubham-sharmas
shubham-sharmas / node-js-complex-cpu-intensive-operation.js
Last active November 17, 2023 18:19
This gist demonstrates a CPU-intensive operation using a Node.js Express server with two endpoints: '/non-blocking-operation' and '/complex-blocking-operation'. If we attempt to run the second one then the first one in parallel, the first operation will be blocked until the second one completes. This illustrates that Node.js CPU-intensive operat…
const express = require("express");
const app = express();
// Function to check if a number is prime.
function isPrime(num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 === 0 || num % 3 === 0) return false;
for (let i = 5; i * i <= num; i += 6) {
if (num % i === 0 || num % (i + 2) === 0) return false;
@shubham-sharmas
shubham-sharmas / hash_worker.js
Created October 30, 2023 15:43
Node.js child_process.fork() method example: forking hash_worker.js to generate password hash using node.js crypto module
const crypto = require('crypto');
process.on('message', (password) => {
crypto.randomBytes(32, (err, salt) => {
if (err) {
process.send({ error: err.message });
} else {
crypto.pbkdf2(password, salt, 10000, 64, 'sha512', (err, derivedKey) => {
if (err) {
process.send({ error: err.message });
@shubham-sharmas
shubham-sharmas / add_numbers.py
Created October 30, 2023 15:22
Node.js child_process.execFile() method example to add two numbers using python script
import sys
def add_numbers(number1, number2):
try:
result = float(number1) + float(number2)
print(result)
except ValueError:
print("Invalid input. Please provide valid numbers!!!")
if __name__ == "__main__":
@shubham-sharmas
shubham-sharmas / nodejs_child_process_exec_method.js
Created October 30, 2023 15:12
Node.js child_process.exec() method example
const { exec } = require('child_process');
exec('ls -l -a', { cwd: process.env.HOME }, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
@shubham-sharmas
shubham-sharmas / nodejs_child_process_spawn_method.js
Last active October 30, 2023 19:00
Node.js child_process.spawn() method example
const { spawn } = require('child_process');
const shellProcess = spawn('ls', ['-l', '-a']);
shellProcess.stdout.on('data', (data) => {
console.log(`Shell Output: ${data}`);
});
shellProcess.stderr.on('data', (data) => {
console.log(`Shell Error: ${data}`);
});
@shubham-sharmas
shubham-sharmas / sequelize-issue-reproduction-11669.js
Last active October 6, 2023 18:47
sequelize-issue-reproduction-11669
/*
Before running the program below, please ensure that you have set up Sequelize on your local system.
You can follow the instructions provided in this link:
https://github.com/sequelize/sequelize/blob/main/CONTRIBUTING.md#how-to-prepare-a-development-environment-for-sequelize
Alternatively, you can run the following commands to set up Sequelize:
git clone https://github.com/sequelize/sequelize.git
cd sequelize
yarn install
yarn start-postgres-latest