Skip to content

Instantly share code, notes, and snippets.

@mLuby
mLuby / what-forces-layout.md
Created January 28, 2016 04:59 — forked from paulirish/what-forces-layout.md
What forces layout/reflow. The comprehensive list.

What forces layout / reflow

All of the below properties or methods, when requested/called in JavaScript, will trigger the browser to synchronously calculate the style and layout*. This is also called reflow or layout thrashing, and is common performance bottleneck.

Element

Box metrics
  • elem.offsetLeft, elem.offsetTop, elem.offsetWidth, elem.offsetHeight, elem.offsetParent
  • elem.clientLeft, elem.clientTop, elem.clientWidth, elem.clientHeight
  • elem.getClientRects(), elem.getBoundingClientRect()
@mLuby
mLuby / home-brewed redux.js
Last active July 26, 2016 21:05
Implementation of redux pattern and simulated use within a react-like app.
"use strict"
// Redux
const bindActionCreators = (actionCreator, dispatch) => args => dispatch(actionCreator(args))
const createStore = reducer => { // (state -> action -> state) -> Store
let state = null
const subscribers = []
const dispatch = action => {
state = reducer(state, action)
@mLuby
mLuby / kill-by-proc-name.sh
Created September 2, 2016 21:03
kill a process by name
# where process has name thing1...
ps aux | grep [t]hing1 | awk '{print $2}' | xargs kill
@mLuby
mLuby / sed-newline.sh
Created September 2, 2016 21:04
sed newline
# in data2.js replace , with ,\n then dump into data.js
cat data2.js | sed -e 's/,/,\'$'\n/g' > data.js
@mLuby
mLuby / rename.sh
Created September 2, 2016 21:05
rename files en-masse
IFS=$'\n'; # change delimiter so it works on all but newline filenames
for file in $(find ./ -name '*.js');
do mv "${file}" "${file}x" # file%jsx removes jsx from end of file
done
for file in $(find ./ -name '*spec.jsx');
do mv "${file}" "${file/unit.spec.jsx/spec.js}" # file%jsx removes jsx from end of file
done
@mLuby
mLuby / if.sh
Created September 2, 2016 21:07
if global var
BOB=true
echo $BOB
if $BOB; then
BOB=false
echo $BOB
fi
@mLuby
mLuby / git-tag-rename.sh
Created September 2, 2016 21:07
rename git tags
# tag a specific commit
git tag v1.7.1 4e13624
# rename a tag
old="1.7.1"
new="v1.7.1"
echo "$old -> $new"
git tag $new $old
git tag -d $old # delete the old tag locally
git push origin :refs/tags/$old # delete the old tag on remote
@mLuby
mLuby / raceWithTimeout.js
Last active January 19, 2017 20:39
Race all promises once and resolve with the first resolved promise or reject with the last error. Useful for connecting.
"use strict"
// race all promises once and resolve with the first resolved promise or reject with the last error.
function raceWithTimeout (promiseList) {
const TIMEOUT = 50
return Promise.race([
...promiseList.map(suppressReject), // if any resolve, resolve with that result.
lastRejectionWithTimeout(promiseList, TIMEOUT) // if all reject, reject with last error; if timeout, reject with latest error.
])
}
@mLuby
mLuby / .eslintrc.json
Last active January 29, 2017 03:58
Comprehensive JS lint file (+ es6, react)
{
"rules": { // And why they're best practice (alphabetized).
"accessor-pairs": [2, {"getWithoutSet": true}], // omission is usually by mistake.
"array-bracket-spacing": 2, // spaces can make arrays take up a lot of space, and concise code allows for more reading context.
"array-callback-return": 2, // omission is usually by mistake.
"arrow-body-style": [2, "as-needed"], // improves consistency and readability.
// "arrow-parens": [2, "as-needed"], // allows for concise arrows while improving consistency.
"arrow-spacing": 2, // improves consistency and readability.
"block-scoped-var": 2, // can behave unexpectedly due to variable hoisting.
"block-spacing": 2, // helps differentiate blocks (spaced) from objects (not spaced).
@mLuby
mLuby / dedup-unique.js
Created February 9, 2017 00:34
one-liner to remove duplicate values from a list in JS. (immutable)
const dedup = list => list.reduce((uniques, item) => uniques.concat(uniques.includes(item) ? [] : [item]), [])
dedup(["a",2,"a",3,"b",2]) // ["a", 2, 3, "b"]