Skip to content

Instantly share code, notes, and snippets.

@lahmatiy
lahmatiy / results.md
Last active October 8, 2017 20:47
JS perf quiz

Similar solutions with very different execution times. What's the reason? Why solution #1 is too slow?

Hint: access to .length property and to variables is not an issue.

Based on a real case in CSSTree

Node v6.11.3

N Solution #1 Solution #2
//
// Case 1
//
var a = [];
var t = performance.now();
for (var i = 0; i < 10000; i++)
a.push(new Promise(function(){}));
// Unique with use object
function uniqueObject(array) {
var hash = Object.create(null);
var result = [];
var value;
for (var i = 0; i < array.length; i++) {
value = array[i];
if (!hash[value]) {
hash[value] = true;
@lahmatiy
lahmatiy / createBlockClass.js
Created July 27, 2016 13:17
Implementation of `model` and `visible` for from fields in basis.js
var events = require('basis.event');
var resolveValue = require('basis.data').resolveValue;
module.exports = function createBlockClass(BaseClass, ext) {
return BaseClass.subclass(ext, {
visible: true,
visibleRA_: null,
emit_visibleChanged: events.create('visibleChanged'),
// value could be a function or binding-bridge instance; any other converts to bool
setVisible: function(value) {
@lahmatiy
lahmatiy / bem-scopes-example.js
Last active April 16, 2016 10:57
Example of css scopes building
var csso = require('csso');
function splitByScope(css) {
var scopes = {};
csso.walk(csso.parse(css), function(node) {
if (node.type === 'Class') {
var className = node.name;
var scopeId = className.replace(/^([^_]+)_.+/, '$1'); // scopeId is block name
@lahmatiy
lahmatiy / refs.js
Created September 10, 2015 20:59
Find the difference
// react 0.14-rc1
var Zoo = React.createClass({
render: function() {
return <div>Giraffe name: <input ref="giraffe" /></div>;
},
showName: function() {
// Previously: var input = this.refs.giraffe.getDOMNode();
var input = this.refs.giraffe;
alert(input.value);
}
@lahmatiy
lahmatiy / AbstractDataset#compute.js
Created July 27, 2013 14:51
AbstractDataset#compute
basis.require('basis.event');
basis.require('basis.data');
var datasetComputeFunctions = {};
basis.data.AbstractDataset.extend({
/**
* @param {string|Array.<string>=} events
@lahmatiy
lahmatiy / patch.js
Created July 9, 2013 23:51
Path for field resolving in basis.data.value.BindValue#addLink and basis.data.value.BindValue#removeLink (resolving was dropped in 0.9.5)
basis.require('basis.data.value');
basis.require('basis.dom');
var DOM = basis.dom;
var BindValue = basis.data.value.BindValue;
var DOM_INSERT_HANDLER = function(value){
DOM.insert(DOM.clear(this), value);
};
var ids = String(
this.req.param('id') ||
this.req.param('ids')
).match(/(\d+)/g);
@lahmatiy
lahmatiy / knowhow_indexFieldOnEntityNestedDatasets.js
Last active December 19, 2015 09:19
HOWTO: Do you know that you can easily create dataset index fields for nested datasets for Entity?
basis.require('basis.entity');
basis.require('basis.data.index');
// create type with single field, that used for EntitySet
var Item = basis.entity.createType('SomeType', {
value: Number
});
// main Entity type
var DemoType = basis.entity.createType('DemoType', {