Code Monkey home page Code Monkey logo

lazy's Introduction

Lazy

License: MIT GitHub go.mod Go version of a Go module GoDoc reference example Go Report Card CircleCI

Simple generic concurrent-safe lazy initialization container for Go.

Installation

go get -u github.com/mono83/lazy

Usage

import "github.com/mono83/lazy"

// Static value supplier
greet := lazy.Const("Hello, world")
fmt.Println(greet())

// With supplier
some := lazy.New(func() any { /* Some heavy operations be execute once */ })
value := some()

// Also there are constructors for value + error tuples
lazy.ConstE[T](42)                // -> func() (T,error)
lazy.Error[T](nil)                // -> func() (T,error)
lazy.NewE(func(T any, err error)) // -> func() (T,error)

Benchmarks

$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: github.com/mono83/lazy
cpu: Intel(R) Core(TM) i5-10400F CPU @ 2.90GHz
BenchmarkConst-12       61397071                20.70 ns/op           24 B/op          1 allocs/op
BenchmarkNew-12         18599815                66.76 ns/op           72 B/op          3 allocs/op
BenchmarkAll/Read-const-1times-12               522478002                2.310 ns/op           0 B/op          0 allocs/op
BenchmarkAll/Read-new-1times-12                 422763762                2.863 ns/op           0 B/op          0 allocs/op
BenchmarkAll/Read-const-100times-12              7491662               162.9 ns/op             0 B/op          0 allocs/op
BenchmarkAll/Read-new-100times-12                4521046               260.5 ns/op             0 B/op          0 allocs/op
BenchmarkAll/Read-const-1000times-12              748990              1563 ns/op               0 B/op          0 allocs/op
BenchmarkAll/Read-new-1000times-12                476796              2619 ns/op               0 B/op          0 allocs/op
BenchmarkAll/Read-const-10000times-12              78136             15567 ns/op               0 B/op          0 allocs/op
BenchmarkAll/Read-new-10000times-12                46693             26274 ns/op               0 B/op          0 allocs/op
PASS
ok      github.com/mono83/lazy  14.628s

lazy's People

Contributors

gotterdemarung avatar

Watchers

 avatar

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.