Skip to content

Instantly share code, notes, and snippets.

@Dammmien
Dammmien / concat_and_dedupe.js
Last active December 15, 2015 17:00
Concatenate two arrays and remove duplicate
// if no duplicate in the first list
a1.concat( a2.filter( i => a1.indexOf( i ) === -1 ) );
// if duplicate in the first list
a1.concat( a2 ).reduce( function( p, c, i, a ) {
if ( p.indexOf( c ) === -1 ) p.push( c );
return p;
}, [] );
@Dammmien
Dammmien / shuffle.js
Created December 14, 2015 14:59
Shuffle an array with the Fisher–Yates shuffle in javascript
function shuffle( a ) {
var m = a.length, t, i;
while ( m ) {
i = Math.floor( Math.random() * m-- );
t = a[ m ];
a[ m ] = a[ i ];
a[ i ] = t;
}
@Dammmien
Dammmien / chunkString.js
Created December 11, 2015 14:30
Split a string into chunks
"abcdefghijklmnopqrstuvwxyz".match( /.{1,4}/g );
// [ "abcd", "efgh", "ijkl", "mnop", "qrst", "uvwx", "yz" ]
@Dammmien
Dammmien / chunk.js
Last active September 30, 2016 09:14
Split an array into chunks
let partition = ( a, n ) => a.length ? [ a.splice( 0, n ), ...partition( a, n ) ] : [];
partition( [ 1, 2, 3, 4, 5, 6, 7, 8 ], 2 );
// [
// [ 1, 2 ],
// [ 3, 4 ],
// [ 5, 6 ],
// [ 7, 8 ]
// ]
@Dammmien
Dammmien / Matrix.js
Last active July 9, 2023 17:33
Easily create an empty matrix in javascript
var matrix = [],
H = 4, // 4 rows
W = 6; // of 6 cells
for ( var y = 0; y < H; y++ ) {
matrix[ y ] = [];
for ( var x = 0; x < W; x++ ) {
matrix[ y ][ x ] = "foo";
}
}
@Dammmien
Dammmien / average.js
Last active October 3, 2019 09:06
Average of an array ES6
var average = arr => arr.reduce( ( p, c ) => p + c, 0 ) / arr.length;
average( [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ] );
// 5
@Dammmien
Dammmien / bfs.js
Last active February 22, 2022 11:35
Breadth First Search (BFS) Graph Traversal in Javascript
var nodes = [
{
links: [ 1 ], // node 0 is linked to node 1
visited: false
}, {
links: [ 0, 2 ], // node 1 is linked to node 0 and 2
path: [],
visited: false
},
...
@Dammmien
Dammmien / time_tracker.js
Last active September 30, 2016 13:24
TimeTracker
class TimeTracker {
constructor() {
this.timers = {};
}
logLoadTimes() {
var loadTimes = chrome.loadTimes();
console.log( `commitLoadTime .............. ${( loadTimes.commitLoadTime - loadTimes.startLoadTime ).toFixed( 2 )}s` );
console.log( `finishDocumentLoadTime ...... ${( loadTimes.finishDocumentLoadTime - loadTimes.startLoadTime ).toFixed( 2 )}s` );
@Dammmien
Dammmien / loadTime.js
Created October 10, 2014 15:09
Log load time in Google Chrome
(function() {
console.clear();
var loadTimes = chrome.loadTimes();
console.log( "requestTime................. " + ( loadTimes.startLoadTime - loadTimes.requestTime ).toFixed( 2 ) + 's' );
console.log( "commitLoadTime.............. " + ( loadTimes.commitLoadTime - loadTimes.startLoadTime ).toFixed( 2 ) + 's' );
console.log( "finishDocumentLoadTime...... " + ( loadTimes.finishDocumentLoadTime - loadTimes.startLoadTime ).toFixed( 2 ) + 's' );
console.log( "finishLoadTime.............. " + ( loadTimes.finishLoadTime - loadTimes.startLoadTime ).toFixed( 2 ) + 's' );
})()
@Dammmien
Dammmien / super_dom.js
Last active August 29, 2015 14:07
Super DOM
var SD = {
create: function( tag, attrs ) {
tag = document.createElement( tag );
for ( var attr in attrs ) tag[ attr ] = attrs[ attr ];
return tag;
},
get: function( selector, context, undefined ) {
var matches = {