Skip to content

Commit

Permalink
replaced fallbackimplementation with es5-shim
Browse files Browse the repository at this point in the history
  • Loading branch information
fbuecklers committed Mar 11, 2012
1 parent 905b788 commit 541a44e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 19 deletions.
19 changes: 13 additions & 6 deletions class.js
Expand Up @@ -5,6 +5,8 @@
* Copyright 2012, Florian Buecklers
* Licensed under the MIT license.
*/
var fakePrototype = Object.getPrototypeOf({constructor: String}) == String.prototype;

if (!Function.prototype.extend) {
Function.prototype.extend = function(target, props) {
if (!props) {
Expand All @@ -25,7 +27,7 @@ Object.extend(Function.prototype, {
linearizedTypes: [Object],
inherit: function() {
var klass = function(toCast) {
if (!(this instanceof klass)) return toCast.isInstanceOf(klass)? toCast: null;
if (!(this instanceof klass)) return toCast && toCast.isInstanceOf(klass)? toCast: null;
this.initialize.apply(this, arguments);
};

Expand Down Expand Up @@ -112,7 +114,13 @@ Object.extend({
proto = Object.create(proto);
proto.constructor = cls;

cls.prototype = proto;
if (fakePrototype) {
cls.wrappedPrototype = proto;
cls.prototype = Object.create(proto);
} else {
cls.prototype = proto;
}

cls.linearizedTypes = linearizedTypes;

return proto;
Expand Down Expand Up @@ -146,8 +154,8 @@ Object.extend(Object.properties, {

Object.extend(Object.basePrototype, {
initialize: function() {},
superCall: function() {
var caller = arguments.callee.caller;
superCall: function superCall() {
var caller = superCall.caller || arguments.callee.caller;

if (caller && caller.methodName) {
var methodName = caller.methodName;
Expand Down Expand Up @@ -178,8 +186,7 @@ Object.extend(Object.basePrototype, {
}
});

if (Object.create)
Trait = Object.inherit({});
var Trait = Object.inherit({});

function classOf(object) {
return Object.getPrototypeOf(Object(object)).constructor;
Expand Down
10 changes: 1 addition & 9 deletions fallback.js
Expand Up @@ -117,13 +117,5 @@

Trait = Object.inherit({});

if (![].__proto__) {
var inherit = Function.prototype.inherit;
Function.prototype.inherit = function() {
var cls = inherit.apply(this, arguments);
cls.wrappedPrototype = cls.prototype;
cls.prototype = Object.create(cls.prototype);
return cls;
};
}

})();
3 changes: 1 addition & 2 deletions index.html
Expand Up @@ -3,13 +3,12 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript" src="lib/es5-shim.js"></script>
<script type="text/javascript" src="class.js"></script>
<script type="text/javascript" src="fallback.js"></script>
<script type="text/javascript" src="test/model.js"></script>
<script type="text/javascript" src="EventHandler.js"></script>
<script type="text/javascript">


var MyEventHandlerClass = Object.inherit(EventHandler, {
_bla: 4,
huhu: 'muh',
Expand Down
2 changes: 1 addition & 1 deletion test/jsTestDriver.conf
@@ -1,8 +1,8 @@
server: http://localhost:9876

load:
- lib/es5-shim.min.js
- class.js
- fallback.js
- test/model.js

test:
Expand Down
1 change: 0 additions & 1 deletion test/model.js
@@ -1,4 +1,3 @@
var start = new Date().getTime();
var TraitA = Trait.inherit({
initialize: function() {
this.fieldA = true;
Expand Down
82 changes: 82 additions & 0 deletions test/perf.html
@@ -0,0 +1,82 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Performance Test</title>
</head>
<script type="text/javascript" src="../class.js"></script>
<script type="text/javascript" src="../fallback.js"></script>
<script type="text/javascript">
var start = new Date().getTime();
</script>
<script type="text/javascript" src="model.js"></script>
<script type="text/javascript">
"use strict";
console.log('init: ' + (new Date().getTime() - start));

var objs = new Array(100000);
start = new Date().getTime();
for (var i = 0; i < 100000; ++i) {
objs[i] = new ClassB();
}

console.log('create: ' + (new Date().getTime() - start));

start = new Date().getTime();
for (var i = 0; i < 100000; ++i) {
objs[i].a();
}

console.log('a: ' + (new Date().getTime() - start));

start = new Date().getTime();
for (var i = 0; i < 100000; ++i) {
objs[i].b();
}

console.log('b: ' + (new Date().getTime() - start));

start = new Date().getTime();
var sum = 0;
for (var i = 0; i < 100000; ++i) {
objs[i].c();
}

console.log('c: ' + sum + ' ' + (new Date().getTime() - start));

start = new Date().getTime();
var sum2 = 0;
for (var i = 0; i < 100000; ++i) {
objs[i].d();
}

console.log('d: ' + sum2 + ' ' + (new Date().getTime() - start));

start = new Date().getTime();
var sum2 = 0;
for (var i = 0; i < 100000; ++i) {
objs[i].e();
}

console.log('e: ' + sum2 + ' ' + (new Date().getTime() - start));

start = new Date().getTime();
var sum2 = 0;
for (var i = 0; i < 100000; ++i) {
objs[i].f();
}

console.log('f: ' + sum2 + ' ' + (new Date().getTime() - start));

start = new Date().getTime();
var result = true;
for (var i = 0; i < 100000; ++i) {
result &= TraitA(objs[i]) != null;
}

console.log('isA: ' + result + ' ' + (new Date().getTime() - start));
</script>
<body>

</body>
</html>

0 comments on commit 541a44e

Please sign in to comment.