Code Monkey home page Code Monkey logo

Comments (2)

a-h avatar a-h commented on July 23, 2024

Did a quick test.

package main

import (
	"crypto/sha256"
	"fmt"
	"io"
	"log"
	"os"
	"time"

	"github.com/a-h/templ/generator"
	"github.com/a-h/templ/parser/v2"
)

func main() {
	var err error
	var now time.Time

	now = time.Now()
	err = hashPath()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("hashPath: ms: %d\n", time.Since(now).Milliseconds())

	now = time.Now()
	err = generatePath()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("generatePath: ms: %d\n", time.Since(now).Milliseconds())
}

func hashPath() (err error) {
	f, err := os.Open("template.templ")
	if err != nil {
		return
	}
	defer f.Close()
	h := sha256.New()
	if _, err = io.Copy(h, f); err != nil {
		return err
	}
	fmt.Printf("%x\n", h.Sum(nil))
	return err
}

func generatePath() (err error) {
	t, err := parser.Parse("template.templ")
	if err != nil {
		return
	}
	outputFileName := "template_temp.go"
	outputFile, err := os.Create(outputFileName)
	if err != nil {
		return
	}
	_, err = generator.Generate(t, outputFile)
	return err
}
0 0 /Users/adrian/github.com/a-h/templ/generator/test-complex-attributes % ./hash
0f3bd5fb1a37ce30dce13849dfb40009dda13e742bb10cb394012b73adb4fe9f
hashPath: ms: 0
generatePath: ms: 5

Although there would be a small overhead in maintaining the hash list, it's likely faster overall to skip generating files that haven't changed by recalculating the hash.

But only by around 5ms per file.

from templ.

a-h avatar a-h commented on July 23, 2024

Fixed in #366

from templ.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.