Skip to content

Instantly share code, notes, and snippets.

View joshblack's full-sized avatar

Josh Black joshblack

View GitHub Profile
[1, 2, 3, 4, 5].duplicate();
// My initial attempt
Array.prototype.duplicate = function() {
var arr = this.slice(0);
arr.forEach(function(e) {
arr.push(e);
});
@joshblack
joshblack / semaphores.c
Created October 26, 2014 19:00
semaphore solution for OS exam
#define N // number of buffers
int in = 0, out = 0; // Reader and writer pointers to buffer spaces
// Semaphores
resources = 0; // number of resources that we are producing
buffer = N; // buffer space
mutex = 1; // Mutual exclusion to make sure we aren't reading and writing at the same time
criticalA = 1; // critical A and B are used to enforce the requirement that we produce A, then B, then A, etc...
criticalB = 1;
@joshblack
joshblack / watchify-reactify-gulp.js
Created November 20, 2014 03:00
Gulpfile for doing watchify builds with the react transformer all hooked up to browser sync.
var gulp = require('gulp');
var util = require('gulp-util');
var watchify = require('watchify');
var reactify = require('reactify');
var browserify = require('browserify');
var source = require('vinyl-source-stream');
var browserSync = require('browser-sync');
var reload = browserSync.reload;
@joshblack
joshblack / in-place-shuffle.js
Last active April 3, 2019 21:42
In-place shuffle algorithm
/**
* Write a function for doing an in-place shuffle of an array.
*
* The shuffle must be "uniform," meaning each item in the
* original array must have the same probability of ending up
* in each spot in the final array.
*
* Must be done in O(n) time and O(1) space
*/
@joshblack
joshblack / semi-private-members.js
Created December 15, 2014 18:48
Semi-private members in JavaScript using Symbols
function Constructor() {
const privateMember = Symbol('privateMember'),
publicMember = 'publicMember';
this[privateMember] = 'foo';
this[publicMember] = 'bar';
}
let c = new Constructor();
function User(username) {
const secretPasswordKey = Symbol('password');
this.username = username;
this.setPassword = function setPassword(password) {
this[secretPasswordKey] = password;
};
this.authenticate = function authenticate(attempt) {
@joshblack
joshblack / closure-example.js
Last active August 29, 2015 14:12
Example of how you can use closure to expose private members
function Closure() {
var number = 0;
// Manipulate the private member directly
function makeLower() {
number--;
console.log(number);
}
// Manipulate the private member directly
@joshblack
joshblack / AnimationPerformance.md
Last active August 29, 2015 14:12
Useful Front-End Stuff

Animation Performance Notes

The Rules (learn them so you can break them!)

  1. No frame should exceed 16ms
  • Caveat: really only the duration between when an animation starts and ends do we need 60fps
  • Do expensive operations before and after an animation
  • Only Need to Worry About Animation Performance in terms of FPS
  1. Never trigger a forced sync layout (layout thrashing)
  • Case Study
el.addEventListener('click', function (e) {
e.target.style.display = 'none';
});
// Instead of using jQuery's $('selector') for grabbing an element, just use these
var elem = document.querySelector('selector'); // Returns a DOM Node
var elems = document.querySelectorAll('selector'); // Returns a nodelist
// Then for event listeners it's just:
elem.addEventListener('click', function (e) {