Permalink
Browse files

cache grammar

  • Loading branch information...
1 parent 93dbfeb commit eb0b970d91f173acf31e9a1bb1dd4e4e138be30e @yiminghe yiminghe committed Jul 25, 2012
Showing with 8 additions and 4 deletions.
  1. +5 −3 src/kison/demo/calculator.html
  2. +3 −1 src/kison/demo/full-calculator.html
@@ -24,6 +24,7 @@
</div>
<script src="../../../build/kissy.js"></script>
<script>
+ var MAPS = {};
KISSY.use("kison", function (S, KISON) {
S.io({
url:'cal.js',
@@ -36,10 +37,11 @@
var $ = S.all;
$("#p").on("click", function () {
var start = S.now();
- var g = new KISON.Grammar(eval(S.trim($("#grammar").val())));
- var ret=g.parse($("#source").val());
+ var grammar = S.trim($("#grammar").val());
+ var g = MAPS[grammar] || (MAPS[grammar] = new KISON.Grammar(eval(grammar)));
+ var ret = g.parse($("#source").val());
alert("耗时: " + (S.now() - start) + "毫秒");
- alert("最终结果:"+ret);
+ alert("最终结果:" + ret);
});
});
</script>
@@ -24,6 +24,7 @@
</div>
<script src="../../../build/kissy.js"></script>
<script>
+ var MAPS = {};
KISSY.use("kison", function (S, KISON) {
S.io({
url:'full-cal.js',
@@ -36,7 +37,8 @@
var $ = S.all;
$("#p").on("click", function () {
var start = S.now();
- var g = new KISON.Grammar(eval(S.trim($("#grammar").val())));
+ var grammar = S.trim($("#grammar").val());
+ var g = MAPS[grammar] || (MAPS[grammar] = new KISON.Grammar(eval(grammar)));
var ret=g.parse($("#source").val());
alert("耗时: " + (S.now() - start) + "毫秒");
alert("最终结果:"+ret);

0 comments on commit eb0b970

Please sign in to comment.