Skip to content

Instantly share code, notes, and snippets.

View AbdelrahmanHafez's full-sized avatar
🏠
Working from home

Hafez AbdelrahmanHafez

🏠
Working from home
  • Mobiquity Inc
  • Amsterdam, Netherlands
View GitHub Profile
@AbdelrahmanHafez
AbdelrahmanHafez / refactoring.coding.challenge.js
Last active December 24, 2023 05:50
Refactoring coding challenge
// The code snippet below is functional, but is made ugly on purpose
// Please refactor it to a state you'd be satisfied with and send back the refactored code
// Bonus challenge: there is a simple change that will improve database writes drastically
// Can you spot it?
const startCronJob = require('nugttah-backend/helpers/start.cron.job');
const Helpers = require('nugttah-backend/helpers');
const Invoice = require('nugttah-backend/modules/invoices');
const DirectOrder = require('nugttah-backend/modules/direct.orders');
const Part = require('nugttah-backend/modules/parts');
@AbdelrahmanHafez
AbdelrahmanHafez / mongoose.save.vs.bulk.save.js
Last active November 18, 2021 16:46
Performance comparison between mongoose `Document#save(...)` and `Model.bulkSave(...)`
import mongoose from 'mongoose';
const { Schema } = mongoose;
const ALL_BANK_ACCOUNTS_COUNT = 100_000;
start().catch(console.error);
async function start () {
await runExperiment(bulkSaveDocuments);
await runExperiment(saveDocumentsIndividually);
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": [
{
"type": "prompt",
"alignment": "left",
"segments": [
{
"type": "path",
"style": "powerline",
@AbdelrahmanHafez
AbdelrahmanHafez / 65831219.js
Last active January 21, 2021 20:33
Comparing bulkWrite performance with all `updateOne`s vs using multiple `updateMany` grouped by the same `update` object.
// https://stackoverflow.com/questions/65831219/mongodb-bulkwrite-multiple-updateone-vs-updatemany?noredirect=1#comment116399692_65831219
'use strict';
const mongoose = require('mongoose');
const { Schema } = mongoose;
const DOCUMENTS_COUNT = 1_000_000;
const UPDATE_MANY_OPERATIONS_COUNT = 100;
const MINIMUM_BALANCE = 0;
const MAXIMUM_BALANCE = 100;
const SAMPLES_COUNT = 10;
@AbdelrahmanHafez
AbdelrahmanHafez / medium.optional.chaining.bad.usage.example.js
Created October 2, 2019 05:26
Medium Optional Chaining Bad Usage Example
// on the user schema, the address, the city, and the name are all required
const user = await User.findOne(filter);
// the correct way of accessing the city name
const name = user.address.city.name;
// the way of accessing the name using optional chaining
const name = user?.address?.city?.name;
@AbdelrahmanHafez
AbdelrahmanHafez / medium.optional.chaining.example.js
Last active October 2, 2019 05:17
Medium optional chaining example
// When you have a deeply nested object like that
// *and you're not sure* whether or not a property exists
const user = {
address: {
city: {
name: 'Tokyo'
}
}
};
@AbdelrahmanHafez
AbdelrahmanHafez / mongodb.memory.server.debug.log
Created June 19, 2018 05:23
Log to help debugging issue with mongodb memory server running slower than expected
Mongo[3568] Starting MongoDB instance with following options: {"port":3568,"dbName":"786c6bf3-3e07-44d8-b297-f03ad6c1a0c0","uri":"mongodb://localhost:3568/786c6bf3-3e07-44d8-b297-f03ad6c1a0c0","storageEngine":"ephemeralForTest","dbPath":"C:\\Users\\Hafez\\AppData\\Local\\Temp\\mongo-mem-10180SoWtC2h2tqGu"} +0ms
Mongo[3568] MongoBinary: Mongod binary path: C:\Users\Hafez\.mongodb-binaries\3.4.4\mongod.exe +75ms
Mongo[3568] MongoBinary: Download lock removed +29ms
Mongo[3568] 2018-06-12T22:51:27.281+0200 I CONTROL [initandlisten] MongoDB starting : pid=3924 port=3568 dbpath=C:\Users\Hafez\AppData\Local\Temp\mongo-mem-10180SoWtC2h2tqGu 64-bit host=Hafez
Mongo[3568] +269ms
Mongo[3568] 2018-06-12T22:51:27.281+0200 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
Mongo[3568] 2018-06-12T22:51:27.281+0200 I CONTROL [initandlisten] db version v3.4.4
Mongo[3568] 2018-06-12T22:51:27.281+0200 I CONTROL [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
Mong