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

Helpers as nodes #1

Merged
merged 2 commits into from
Jan 4, 2014
Merged

Helpers as nodes #1

merged 2 commits into from
Jan 4, 2014

Conversation

thekid
Copy link
Member

@thekid thekid commented Jan 3, 2014

This pull request changes the block helpers from builtin helpers over to specialized BlockHelper nodes, and boosts performance enormously:

<?php
use util\profiling\Timer;
use com\handlebarsjs\HandlebarsEngine;
use com\github\mustache\DataContext;

class Benchmark extends \util\cmd\Command {

  public function run() {

    // Fixtures
    $data= array('person' => array('name' => 'World'));
    $template= 'Hello, {{#if person}}{{#with person}}{{name}}{{/with}}{{/if}}';
    $times= 100000;

    // Code
    $engine= new HandlebarsEngine();
    $compiled= $engine->compile($template);
    $context= create(new DataContext($data))->withEngine($engine);
    $t= Timer::measure(function() use($times, $engine, $compiled, $context) {
      for ($i= 0; $i < $times; $i++) {
        $compiled->evaluate($context);
      }
    });
    $this->out->writeLine($engine->evaluate($compiled, $context));
    $this->out->writeLinef('%d times, %.3f seconds', $times, $t->elapsedTime());
  }
}

master

$ xpcli Benchmark.class.php
Hello, World
100000 times, 9.802 seconds

helpers-as-nodes

$ xpcli Benchmark.class.php
Hello, World
100000 times, 2.090 seconds

@thekid
Copy link
Member Author

thekid commented Jan 3, 2014

Still roughly for to tive times slower that its NodeJS equivalent, though:

Handlebars= require('handlebars');

// Fixtures
var template = 'Hello, {{#if person}}{{#with person}}{{name}}{{/with}}{{/if}}';
var context = {person : { name : 'World'}};
var times= 100000;

// Code
var compiled = Handlebars.compile(template);
var start= new Date().getTime();
for (var i= 0; i < times; i++) {
  compiled(context);
}
console.log(compiled(context));
console.log('%d runs, %s seconds', times, (new Date().getTime() - start) / 1000);
$ node benchmark.js
Hello, World
100000 runs, 0.489 seconds

thekid added a commit that referenced this pull request Jan 4, 2014
@thekid thekid merged commit c851291 into master Jan 4, 2014
@thekid thekid deleted the helpers-as-nodes branch January 4, 2014 23:08
@ghost ghost assigned thekid Jan 4, 2014
thekid added a commit that referenced this pull request Jul 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant