Wednesday, October 10, 2007

One line of code can make your JavaScript code faster with IE.

I discovered a single line of code that make JavaScript code faster with IE. The line is:

/*@cc_on _d=document;eval('var document=_d')@*/

You can try the next code with IE:

// Before
var date = new Date;
for (var i = 0; i < 100000; i++) document; 
alert(new Date - date);

/*@cc_on _d=document;eval('var document=_d')@*/

// After
date = new Date;
for (var i = 0; i < 100000; i++) document; 
alert(new Date - date);

I show you another code with same concept:

/*@cc_on
eval((function(props) {
  var code = [];
  for (var i = 0 l = props.length;i<l;i++){
    var prop = props[i];
    window['_'+prop]=window[prop];
    code.push(prop+'=_'+prop)
  }
  return 'var '+code.join(',');
})('document self top parent alert setInterval clearInterval setTimeout clearTimeout'.split(' ')));
@*/

Try it!

Monday, October 8, 2007

XPath Functional Test

I wrote XPath Functional Test for HTML.

XPath Functional Test

Now, I found some bugs in XPath Engine of Browsers.

Try it.

Friday, October 5, 2007

Bug in XPath Engine of Safari 3.0

The pipe "|" operator after Predicate is ignored.

Example:

// 2
alert(
  document.evaluate(
    '//body | //head', document, null, 7, null).snapshotLength
);

// expected 2 but actually 1
alert(
  document.evaluate(
    '//body[.] | //head', document, null, 7, null).snapshotLength
);

The "string-after" function gets wrong string.

Example:

// expected "d" actually "cd"
alert(
  document.evaluate(
    'substring-after("abcd","bc")', document, null, 2, null).stringValue
);

Re: JavaScript: Put everything in a namespace

Re: JavaScript: Put everything in a namespace

I always write like:

var Article = Article ? Article : new function() {
  // private
  var title = "Report: School Shootings Help Prepare Students For Being Shot In Real World";

  // public
  this.getTitle = function() {
    return title;
  };
  this.save = function() {
    alert(”Saving ” + this.getTitle());
  }; 
};

Wednesday, October 3, 2007

Better way to cast object to primitive

Let's create class like:

var Klass = function() {}
Klass.prototype {
  string: functioin() { /* return as string */ },
  number: functioin() { /* return as number */ },
  boolean: functioin() { /* return as string */ }
};

This code use like:

var obj = new Klass();
obj[typeof any]() == any; // Yet another type casting

Enjoy!

My first entry

I'm Shibuya-kei Software Engineer!

(function() {
  alert('Hello Blogger!');
})();