Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some performance improvements #1023

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = function(grunt) {
grunt.initConfig({
// Metadata.
pkg: pkg,
banner: grunt.file.read("dev/copy.js").replace(/@VERSION/, pkg.version),
banner: grunt.file.read("dev/banner.txt").replace(/@VERSION/, pkg.version),
// Task configuration.
uglify: {
options: {
Expand All @@ -21,6 +21,14 @@ module.exports = function(grunt) {
nodeps: {
src: "<%= concat.nodeps.dest %>",
dest: "<%= pkg.name %>-nodeps-min.js"
},
novml: {
src: "<%= concat.novml.dest %>",
dest: "<%= pkg.name %>-novml-min.js"
},
nodeps_novml: {
src: "<%= concat.nodeps_novml.dest %>",
dest: "<%= pkg.name %>-nodeps-novml-min.js"
}
},
replace: {
Expand All @@ -34,7 +42,12 @@ module.exports = function(grunt) {
files: [{
expand: true,
flatten: true,
src: ["<%= concat.dist.dest %>", "<%= concat.nodeps.dest %>"],
src: [
"<%= concat.dist.dest %>",
"<%= concat.nodeps.dest %>",
"<%= concat.novml.dest %>",
"<%= concat.nodeps_novml.dest %>"
],
dest: "./"
}]
}
Expand All @@ -58,6 +71,23 @@ module.exports = function(grunt) {
"dev/raphael.vml.js",
"dev/raphael.amd.js"
]
},
novml: {
dest: "<%= pkg.name %>-novml.js",
src: [
"dev/eve.js",
"dev/raphael.core.js",
"dev/raphael.svg.js",
"dev/raphael.amd.novml.js"
]
},
nodeps_novml: {
dest: "<%= pkg.name %>-nodeps-novml.js",
src: [
"dev/raphael.core.js",
"dev/raphael.svg.js",
"dev/raphael.amd.novml.js"
]
}
}
});
Expand Down
5 changes: 5 additions & 0 deletions dev/raphael.amd.novml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
define(["./raphael.core", "./raphael.svg"], function(R) {

return R;

});
68 changes: 49 additions & 19 deletions dev/raphael.core.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
define(["eve"], function(eve) {

var isIE7 = navigator.appVersion.indexOf("MSIE 7.") != -1;

/*\
* Raphael
[ method ]
Expand Down Expand Up @@ -452,8 +454,10 @@ define(["eve"], function(eve) {
- rad (number) angle in radians
= (number) angle in degrees.
\*/
R.deg = function (rad) {
R.deg = isIE7 ? function (rad) {
return Math.round ((rad * 180 / PI% 360)* 1000) / 1000;
} : function (rad) {
return rad * 180 / PI % 360;
};
/*\
* Raphael.snapTo
Expand Down Expand Up @@ -532,7 +536,7 @@ define(["eve"], function(eve) {
bod = createPopup().document.body;
}
var range = bod.createTextRange();
toHex = cacher(function (color) {
toHex = cacher('toHex', function (color) {
try {
bod.style.color = Str(color).replace(trim, E);
var value = range.queryCommandValue("ForeColor");
Expand All @@ -547,7 +551,7 @@ define(["eve"], function(eve) {
i.title = "Rapha\xebl Colour Picker";
i.style.display = "none";
g.doc.body.appendChild(i);
toHex = cacher(function (color) {
toHex = cacher('toHex', function (color) {
i.style.color = color;
return g.doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
});
Expand Down Expand Up @@ -808,24 +812,50 @@ define(["eve"], function(eve) {
return array.push(array.splice(i, 1)[0]);
}
}
function cacher(f, scope, postprocessor) {

var CACHE = R.CACHE = {};

function cacher(name, f, scope, postprocessor) {

function newf() {
var arg = Array.prototype.slice.call(arguments, 0),
args = arg.join("\u2400"),
cache = newf.cache = newf.cache || {},
count = newf.count = newf.count || [];
args = arg.join("\u2400");

if (cache[has](args)) {
repush(count, args);
if (count[count.length - 1] !== args) {
repush(count, args);
}

return postprocessor ? postprocessor(cache[args]) : cache[args];
}

count.length >= 1e3 && delete cache[count.shift()];
count.push(args);
cache[args] = f[apply](scope, arg);
return postprocessor ? postprocessor(cache[args]) : cache[args];
var cached = cache[args] = f[apply](scope, arg);
return postprocessor ? postprocessor(cached) : cached;
}

var cache = newf.cache = {},
count = newf.count = [];

newf.clear = function () {
cache = newf.cache = {};
count = newf.count = [];
};

CACHE[name] = newf;

return newf;
}

R.clearCaches = function () {
for (var key in CACHE) {
if (!CACHE.hasOwnProperty(key)) continue;

CACHE[key].clear();
}
};

var preload = R._preload = function (src, f) {
var img = g.doc.createElement("img");
img.style.cssText = "position:absolute;left:-9999em;top:-9999em";
Expand Down Expand Up @@ -872,7 +902,7 @@ define(["eve"], function(eve) {
o error (boolean) true if string can’t be parsed
o }
\*/
R.getRGB = cacher(function (colour) {
R.getRGB = cacher('getRGB', function (colour) {
if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
return {r: -1, g: -1, b: -1, hex: "none", error: 1, toString: clrToString};
}
Expand Down Expand Up @@ -954,7 +984,7 @@ define(["eve"], function(eve) {
- b (number) value or brightness
= (string) hex representation of the colour.
\*/
R.hsb = cacher(function (h, s, b) {
R.hsb = cacher('hsb', function (h, s, b) {
return R.hsb2rgb(h, s, b).hex;
});
/*\
Expand All @@ -968,7 +998,7 @@ define(["eve"], function(eve) {
- l (number) luminosity
= (string) hex representation of the colour.
\*/
R.hsl = cacher(function (h, s, l) {
R.hsl = cacher('hsl', function (h, s, l) {
return R.hsl2rgb(h, s, l).hex;
});
/*\
Expand All @@ -982,7 +1012,7 @@ define(["eve"], function(eve) {
- b (number) blue
= (string) hex representation of the colour.
\*/
R.rgb = cacher(function (r, g, b) {
R.rgb = cacher('rgb', function (r, g, b) {
function round(x) { return (x + 0.5) | 0; }
return "#" + (16777216 | round(b) | (round(g) << 8) | (round(r) << 16)).toString(16).slice(1);
});
Expand Down Expand Up @@ -1116,7 +1146,7 @@ define(["eve"], function(eve) {
- TString (string|array) transform string or array of transformations (in the last case it will be returned straight away)
= (array) array of transformations.
\*/
R.parseTransformString = cacher(function (TString) {
R.parseTransformString = cacher('parseTransformString', function (TString) {
if (!TString) {
return null;
}
Expand Down Expand Up @@ -1823,7 +1853,7 @@ define(["eve"], function(eve) {
rad = PI / 180 * (+angle || 0),
res = [],
xy,
rotate = cacher(function (x, y, rad) {
rotate = cacher('a2c.rotate', function (x, y, rad) {
var X = x * math.cos(rad) - y * math.sin(rad),
Y = x * math.sin(rad) + y * math.cos(rad);
return {x: X, y: Y};
Expand Down Expand Up @@ -1912,7 +1942,7 @@ define(["eve"], function(eve) {
y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
};
},
curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
curveDim = cacher('curveDim', function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
c = p1x - c1x,
Expand Down Expand Up @@ -1955,7 +1985,7 @@ define(["eve"], function(eve) {
max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
};
}),
path2curve = R._path2curve = cacher(function (path, path2) {
path2curve = R._path2curve = cacher('path2curve', function (path, path2) {
var pth = !path2 && paths(path);
if (!path2 && pth.curve) {
return pathClone(pth.curve);
Expand Down Expand Up @@ -2096,7 +2126,7 @@ define(["eve"], function(eve) {
}
return p2 ? [p, p2] : p;
}, null, pathClone),
parseDots = R._parseDots = cacher(function (gradient) {
parseDots = R._parseDots = cacher('parseDots', function (gradient) {
var dots = [];
for (var i = 0, ii = gradient.length; i < ii; i++) {
var dot = {},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"scripts": {
"start": "npm install && bower install",
"build": "webpack && webpack --min && webpack --no-deps && webpack --no-deps --min",
"build": "webpack && webpack --min && webpack --no-deps && webpack --no-deps --min && webpack --no-vml && webpack --no-vml --min && webpack --no-vml --no-deps && webpack --no-vml --no-deps --min",
"test": "echo \"Open dev/test/index.html with your browser\" && exit 1"
},
"repository": {
Expand Down
9 changes: 8 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const webpack = require("webpack");
const fs = require("fs");

const args = process.argv;
var sourceEntry = './dev/raphael.amd.js';

let plugins = [
new webpack.BannerPlugin(fs.readFileSync('./dev/banner.txt', 'utf8'), { raw: true, entryOnly: true })
Expand All @@ -18,6 +19,12 @@ if(args.indexOf('--no-deps') !== -1){
filename += ".no-deps"
}

if(args.indexOf('--no-vml') !== -1){
console.log('Building version without vml');
sourceEntry = './dev/raphael.amd.novml.js';
filename += ".no-vml"
}

if(args.indexOf('--min') !== -1){
console.log('Building minified version');
plugins.push(
Expand All @@ -32,7 +39,7 @@ if(args.indexOf('--min') !== -1){
}

module.exports = {
entry: './dev/raphael.amd.js',
entry: sourceEntry,
output: {
filename: filename + ".js",
libraryTarget: "umd",
Expand Down