Skip to content

Instantly share code, notes, and snippets.

@marekpiechut
marekpiechut / simple-circuit-breaker.js
Last active January 30, 2017 14:38
Simple circuit breaker in ES6 (available in NPM as simple-circuit-breaker)
const CLOSED = Symbol('CLOSED')
const OPEN = Symbol('OPEN')
const HALF_OPEN = Symbol('HALF_OPEN')
const MSG = 'Functionality disabled due to previous errors.'
module.exports = (asyncFn, gracePeriodMs = 3000, threshold = 1, message = MSG) => {
let state = CLOSED
let failures = 0
let openedAt
function handleSuccess(value) {
if(state !== CLOSED) {
@marekpiechut
marekpiechut / simple-circuit-breaker-usage.js
Created January 30, 2017 14:38
Usage of simple-circuit-breaker
function callBackend(url) {
return fetch(url)
.then(parseResponse)
}
const withCircuitBreaker = circuitBreaker(callBackend)
withCircuitBreaker('http://my.backend.com')
.then(doSomethingWithData, handleError)
@marekpiechut
marekpiechut / closure-factory.js
Last active August 22, 2017 07:23
Simple factory with object literal
const createMyType = (startVal) => {
let name = "Magic Object"
let magic = 32
let val = startVal
return {
add: (b) => { val += magic + b },
sub: (b) => { val += magic - b },
getVal: () => val
}
@marekpiechut
marekpiechut / object-customizer.js
Created August 18, 2017 10:11
Benchmark object customizer
return Object.create(baseObejct, {
name: {
writable: true,
value: "Magic Object",
},
magic: {
writable: true,
value: 32,
},
val: {
@marekpiechut
marekpiechut / proto.js
Created August 18, 2017 11:02
Benchmark - prototypes
window.BENCH = ((target) => {
const MyType = function(startVal) {
this.val = startVal
this.name = "Magic Object"
this.magic = 32
}
MyType.prototype.add = function(b) {
this.val += this.magic + b
return this
@marekpiechut
marekpiechut / proto-small.js
Last active August 18, 2017 11:31
Benchmark proto - small
const MyType = function(startVal) {
this.val = startVal
...
}
MyType.prototype.add = function(b) { ... }
MyType.prototype.sub = function(b) { ... }
...
for(let i = 0; i < 1000000; i++) {
@marekpiechut
marekpiechut / closure-small.js
Last active August 18, 2017 11:37
Benchmark - object closure
const createMyType = (startVal) => {
let val = startVal
let magic = ...
return {
add: (b) => {
val += magic + b
},
sub: (b) => { ... },
@marekpiechut
marekpiechut / sublime-medium-1.js
Created October 29, 2017 15:16
Some very important article
function test(ar) {
return ar2 tratatat
}
if (__DEV__) {
//do dev-only stuff that's not really interesting in production
}
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify('production')
}
})