Skip to content

Instantly share code, notes, and snippets.

Richard Scarrott richardscarrott

Block or report user

Report or block richardscarrott

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@richardscarrott
richardscarrott / test.js
Created Feb 6, 2020
High precision time
View test.js
const work = () => {
for (let i = 0; i < 1000000; i++) {}
};
const start = process.hrtime.bigint();
work();
const end = process.hrtime.bigint();
console.log(
`Benchmark took ${Number(end - start) / 1000 / 1000 / 1000} seconds`
);
@richardscarrott
richardscarrott / closest.ts
Created Jan 24, 2020
closest([1, 2, 6, 0], 5) // 6
View closest.ts
const closest = (values: number[], val: number) => {
if (!values.length) {
throw new VError(
{ info: { values, val } },
'Expected values to contain at least one value'
);
}
return values.reduce(
(acc, currVal) =>
Math.abs(acc.delta) > Math.abs(currVal - val)
View server.js
const express = require('express');
console.log(process.version);
const app = express();
app.get('/hello', (req, res) => {
console.log('HANDLING REQUEST');
res.setHeader('content-type', 'application/json');
res.flushHeaders(); // `flushHeaders` to prevent http client from retrying and therefore invoking this handler multiple times.
@richardscarrott
richardscarrott / index.js
Created Dec 12, 2019
js-string-memory-usage
View index.js
// node --max-old-space-size=100 --expose-gc index.js
const prettyBytes = require('pretty-bytes');
const forceGC = () => {
if (global.gc) {
global.gc();
} else {
console.warn('No GC hook! Start your program as `node --expose-gc file.js`.');
}
@richardscarrott
richardscarrott / Button.jsx
Last active Mar 8, 2018
React Component Design
View Button.jsx
const Button = ({ tagName: TagName = 'button', ...rest }) => <TagName className="button" {...rest} />;
const App = () => {
return (
<>
<Button onClick={() => console.log('clicked')}>renders HTMLButtonElement (default)</Button>
<Button tagName="a" href="http://google.com">renders HTMLAnchorElement (for functional purposes)</Button>
<Button tagName="h3">renders HTMLHeadingElement (for weird SEO purposes)</Button>
</>
);
@richardscarrott
richardscarrott / index.html
Last active Jun 29, 2017
iOS body scroll rubber banding bug
View index.html
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<style>
* {
@richardscarrott
richardscarrott / react-loadable
Created Apr 3, 2017
react-loadable flushModuleIds / flushServerSideRequires
View react-loadable
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.default = Loadable;
@richardscarrott
richardscarrott / !react-enzyme-render-methods.md
Last active Aug 13, 2018
react-enzyme - `shallow` vs `mount` vs `render` lifecycle methods
View !react-enzyme-render-methods.md

An exploration of the different render methods available in react-enzyme.

View add.js
function add(val = 0, ...args) {
let sum = args.reduce((prev, curr) => {
return prev + curr;
}, val);
let ret = add.bind(this, sum);
ret.value = () => sum;
ret.add = ret;
return ret;
}
You can’t perform that action at this time.