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
N | Solution #1 | Solution #2 |
---|
var chalk = require('chalk'); | |
var readline = require('readline'); | |
var BAR_LENGTH = 40; | |
var lines = 0; | |
function repeatStr(str, len){ | |
return new Array(parseInt(len) + 1).join(str); | |
} | |
function drawBarLine(fill, str){ |
// doesn't work w/o this interface creation | |
require('readline').createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
// stop process input | |
process.stdin.pause(); | |
// read from stdin 10 times per second to check if ctrl+c pressed |
// 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); | |
} |
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 |
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) { |
// 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; |
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
N | Solution #1 | Solution #2 |
---|
const EXTENSION_TYPE = { | |
0x01: 'PlainText', | |
0xF9: 'GraphicControl', | |
0xFE: 'Comment', | |
0xFF: 'Application' | |
}; | |
/** | |
* Returns total length of data blocks sequence | |
* |
const pngSignature = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]); | |
const hashKey = 'react-snapshot-hash'; | |
const crcTable = []; | |
const initialCrc = 0xffffffff; | |
for (let n = 0; n < 256; n++) { | |
let c = n; | |
for (let k = 0; k < 8; k++) { | |
if (c & 1) { |
const csstree = require('css-tree'); | |
const data = require('mdn-data/css/properties.json'); | |
Object.keys(data).forEach(name => { | |
const prop = data[name]; | |
if (Array.isArray(prop.initial)) { | |
return; | |
} |