Skip to content

Instantly share code, notes, and snippets.

View morphar's full-sized avatar

Dan Larsen morphar

View GitHub Profile
@morphar
morphar / split.js
Created May 29, 2014 03:31
JavaScript split by string or regex
var str = "bhds ahjds abdjsab dhjsab hdjs abhjds bahjsa\n" +
"bhds ahjds abdjsab dhjsab hdjs abhjds bahjsa\n" +
"bhds ahjds abdjsab dhjsab hdjs abhjds bahjsa\n" +
"bhds ahjds abdjsab dhjsab hdjs abhjds bahjsa\n" +
"bhds ahjds abdjsab dhjsab hdjs abhjds bahjsa\n" +
"bhds ahjds abdjsab dhjsab hdjs abhjds bahjsa\n" +
"bhds ahjds abdjsab dhjsab hdjs abhjds bahjsa\n" +
"bhds ahjds abdjsab dhjsab hdjs abhjds bahjsa\n" +
"bhds ahjds abdjsab dhjsab hdjs abhjds bahjsa\n" +
"bhds ahjds abdjsab dhjsab hdjs abhjds bahjsa\n" +
@morphar
morphar / README.md
Created May 29, 2014 03:11
JavaScript string join vs concatenation

For a long time, I have assumed that joining strings ([line1, line2].join('')), was faster than concatenating (line1 + line2).

Suddenly it hit me, that I actually didn't know if it was true.

So I created this test, which basically CRUSHES that assumption!

This is apparently an old fact, that has just stuck with me.

According to Craig Buckler the assumption is valid in IE7! So if you are writing code for IE7, keep telling yourself to join ;-)

@morphar
morphar / clone.js
Last active December 22, 2015 17:49
Deep cloning of objects in javascript
/**
example:
var params = { fullText: { abc: '123' }, test1: { $in: [1,2,3] }, test2: { $in: ['a','b','c'] } };
var filter = clone(params);
delete filter.fullText;
params.test1.$in[1] = 5;
params.test2.$in[1] = 'x';
console.log(params);
console.log(filter);
@morphar
morphar / classes-vs-functions.js
Created April 23, 2013 12:28
Interesting little experiment... I have seen many assume that "classes" in node.js is slow... This experiment show that the difference between a module which exports multiple functions vs a module that exports a single class, is almost non-existent! If anything, it shows that we shouldn't be afraid to create new instances of "classes"! That was …
var ModClassed = require('./mod-classed');
var modClean = require('./mod-clean');
var modClassed = new ModClassed();
var multiFactor = 10000;
// Doing classed stuff
console.time('classed');
@morphar
morphar / events-tests.js
Last active December 15, 2015 22:59
A quick test to get some rough performance numbers for events. roughly these are the numbers: no events = 52ms, with events + self listener = 432ms, with events but no listeners = 1203ms, with events and checks for listeners = 1081ms. Speed difference from no events to with events are from x 8.3 to x 23.1!!!
var util = require('util');
var events = require('events');
var testRuns = 10000000;
// A class without event emitter
var BlaClass0 = function() { };
BlaClass0.prototype.doCalc = function() {
var bla = 3.14 * 1234567890;
@morphar
morphar / nodejs-bcrypt-compare.js
Last active February 8, 2018 06:55
A quick speed comparison between bcrypt and bcrypt-nodejs for node.js. bcrypt-nodejs is approximately 3 times as slow as bcrypt. If you try this out yourself, be sure to only run 1 test at a time, to get correct results.
var bcrypt = require('bcrypt');
var bcrypt2 = require('bcrypt-nodejs');
var bcryptHashTests = 10;
var bcryptCompareTests = 10;
/* Generate */
console.time('bcrypt-sync-gen');