Skip to content

Instantly share code, notes, and snippets.

View mistakster's full-sized avatar

Vladimir Kuznetsov mistakster

View GitHub Profile
@mistakster
mistakster / README.md
Last active September 13, 2022 01:04
Optimizing performance of the React list component

Optimizing performance of the React list component

Basically, everything need to be done in shouldComponentUpdate() method.

import React, {Component} from 'react';

class Item extends Component {
  shouldComponentUpdate(nextProps, nextState) {
 // always re-render component
@mistakster
mistakster / react-test-case.js
Last active October 22, 2021 13:56
The test case for interview discussion (React developer)
import { useState } from 'react';
// 1) The following code is not optimal in terms
// of performance. Could you improve it?
const Page = () => {
const [value, setValue] = useState(0);
return (
<Counter
value={value}
@mistakster
mistakster / chart.js
Last active September 6, 2020 11:55
dc.js cumulative chart
(function () {
const dateFormat = d3.time.format('%Y-%m');
// produce mock data
function generateData() {
const generator = new MersenneTwister(123456);
const startDate = +new Date('2016-01-01');
const endDate = +new Date('2017-01-01');
return _.range(100)
@mistakster
mistakster / index.js
Created April 6, 2018 06:36
The memory usage logger for a Node.js process
/**
* The memory usage logger for a Node.js process
*
* Resident Set Size (RSS) is the amount of space occupied in the main memory
* device for the process, which includes the heap, code segment and stack.
* Variables are stored in the stack and the actual JavaScript code resides in the code segment.
*
* The heap is where objects, strings, and closures are stored.
*
* @see https://nodejs.org/api/process.html#process_process_memoryusage
@mistakster
mistakster / index.md
Last active August 8, 2017 17:55
Functional tests of the Yandex’s homepage
@mistakster
mistakster / webpack.config.js
Created July 9, 2015 16:23
Webpack configuration file
/**
* Build for production:
* $ NODE_ENV=production webpack
*
* Build for staging or development mode
* $ webpack
*
* Run app server in dev mode and use Hot Module Replacement
* $ NODE_ENV=webpack nodemon --watch ./app index.js
*
@mistakster
mistakster / index.js
Last active April 6, 2017 10:27
The platform independent way to provide environment variables inside “npm scripts” section
const ENV = process.env.NODE_ENV;
@mistakster
mistakster / promisify.js
Created April 6, 2017 10:00
A helper function which wrap any function expecting callback as the last argument and return promise
function toArray(obj) {
return [].slice.apply(obj);
}
module.exports = function promisify(fn) {
return function () {
const args = toArray(arguments);
const self = this;
return new Promise(function (resolve, reject) {
@mistakster
mistakster / postcss-pipeline-webpack-plugin.js
Last active December 30, 2016 06:52
A webpack plugin to process generated assets with PostCSS pipeline. Also, there is an npm version — https://github.com/mistakster/postcss-pipeline-webpack-plugin
const RawSource = require('webpack-sources').RawSource;
const postcss = require('postcss');
const MASK = /\.css$/;
/**
* @param {Function} options.predicate is a function invoked per CSS file
* @param {String} options.suffix is a string attached to the new file
* @param {Array} options.pipeline is a list of PostCSS plugins
* @param {Object} options.map is a PostCSS source maps configuration
// Conver scale factor into pixels
.fontScale(@scale: 0) {
@list: 14px, 16px, 20px, 24px, 28px, 34px, 41px, 50px, 60px;
.test-args(@scale) when (@scale >= -2) and (@scale <= 6) {
font-size: extract(@list, @scale + 3);
}
.test-args(@scale);
}