import { describe, bench } from "vitest"; import { Field, Form } from "houseform"; import React, { useState } from "react"; import { cleanup, fireEvent, render } from "@testing-library/react"; import { Formik, Field as FormikField } from "formik"; import { Controller, useForm } from "react-hook-form"; import { FieldProps } from "formik/dist/Field"; const arr = Array.from({ length: 1000 }, (_, i) => i); function HouseFormOnSubmitBenchmark() { const [val, setVal] = useState | null>(null); if (val) { return (

Value: {JSON.stringify(val)}

); } return ( onSubmit={(values) => { setVal(values); }} > {({ submit }) => ( <> {arr.map((num, i) => { return ( {({ value, onBlur, setValue }) => { return ( setValue(e.target.valueAsNumber)} value={value} /> ); }} ); })} )} ); } function FormikOnSubmitBenchmark() { const [val, setVal] = useState | null>(null); if (val) { return (

Value: {JSON.stringify(val)}

); } return ( { setVal(values); }} > {({ submitForm }) => ( <> {arr.map((num, i) => ( {({ field }: FieldProps) => { return ( ); }} ))} )} ); } function ReactHookFormOnSubmitBenchmark() { const { register, handleSubmit } = useForm({ defaultValues: { num: arr, }, }); const [val, setVal] = useState | null>(null); if (val) { return (

Value: {JSON.stringify(val)}

); } return ( <> {arr.map((num, i) => { return ; })} ); } function ReactHookFormHeadlessOnSubmitBenchmark() { const { control, handleSubmit } = useForm({ defaultValues: { num: arr, }, }); const [val, setVal] = useState | null>(null); if (val) { return (

Value: {JSON.stringify(val)}

); } return ( <> {arr.map((num, i) => { return (

{value}

} name={`num.${i}`} /> ); })} ); } describe("Submits 1,000 form items", () => { bench( "HouseForm", async () => { const { getByText, findByText } = render(); fireEvent.click(getByText("Submit")); await findByText("Value:"); }, { setup(task) { task.opts.beforeEach = () => { cleanup(); }; }, } ); bench( "Formik", async () => { const { getByText, findByText } = render(); fireEvent.click(getByText("Submit")); await findByText("Value:"); }, { setup(task) { task.opts.beforeEach = () => { cleanup(); }; }, } ); bench( "React Hook Form", async () => { const { getByText, findByText } = render( ); fireEvent.click(getByText("Submit")); await findByText("Value:"); }, { setup(task) { task.opts.beforeEach = () => { cleanup(); }; }, } ); bench( "React Hook Form (Headless)", async () => { const { getByText, findByText } = render( ); fireEvent.click(getByText("Submit")); await findByText("Value:"); }, { setup(task) { task.opts.beforeEach = () => { cleanup(); }; }, } ); });